0
0
Fluttermobile~10 mins

Why validation ensures data quality in Flutter - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a simple validation that checks if the input is not empty.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || value[1] '') {
      return 'Please enter some text';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
A!=
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes the validation to fail incorrectly.
Using comparison operators like '>' or '<' on strings causes errors.
2fill in blank
medium

Complete the code to validate that the input length is at least 5 characters.

Flutter
TextFormField(
  validator: (value) {
    if (value != null && value.length [1] 5) {
      return 'Enter at least 5 characters';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' reverses the logic.
Using '>=' or '<=' includes 5, which is not the intended check.
3fill in blank
hard

Fix the error in the validation that checks if the input is a valid email format.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || !RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').[1](value)) {
      return 'Enter a valid email';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
Acontains
Bmatch
ChasMatch
Dtest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' or 'contains' causes errors because they are not RegExp methods.
Using 'test' is from other languages, not Dart.
4fill in blank
hard

Fill both blanks to create a validation that ensures the input is a number between 1 and 10.

Flutter
TextFormField(
  validator: (value) {
    final numValue = int.tryParse(value ?? '');
    if (numValue == null || numValue [1] 1 || numValue [2] 10) {
      return 'Enter a number between 1 and 10';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' includes boundary numbers incorrectly.
Reversing the operators changes the logic.
5fill in blank
hard

Fill all three blanks to create a validation that returns an error if the input is empty, less than 3 characters, or contains spaces.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || value[1] '' || value.length [2] 3 || value.contains([3])) {
      return 'Invalid input';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
A==
B<
C' '
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for empty check.
Using '>=' instead of '<' for length check.
Using wrong character for contains check.