0
0
Fluttermobile~20 mins

Custom validators in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Custom Validator Behavior on Empty Input
Consider a Flutter TextFormField with a custom validator that checks if the input is a valid email. What will the validator return if the input is empty?
Flutter
String? emailValidator(String? value) {
  if (value == null || value.isEmpty) {
    return 'Email is required';
  }
  final emailRegex = RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
  if (!emailRegex.hasMatch(value)) {
    return 'Enter a valid email';
  }
  return null;
}
A'Email is required'
Bnull
C'Enter a valid email'
DThrows an exception
Attempts:
2 left
💡 Hint
Think about what the validator returns when the input is empty or null.
📝 Syntax
intermediate
2:00remaining
Correct Syntax for Custom Validator Function
Which of the following is the correct syntax for a Flutter custom validator function that checks if a password is at least 8 characters?
A
String? passwordValidator(String value) {
  if (value.length < 8) return 'Password too short';
  return null;
}
B
void passwordValidator(String? value) {
  if (value == null || value.length < 8) throw Exception('Password too short');
}
C
bool passwordValidator(String? value) {
  return value != null && value.length >= 8;
}
D
String? passwordValidator(String? value) {
  if (value == null || value.length < 8) return 'Password too short';
  return null;
}
Attempts:
2 left
💡 Hint
Remember the validator must return a String? and accept nullable input.
lifecycle
advanced
2:00remaining
When is a Custom Validator Called in Flutter Forms?
In a Flutter form, when does the custom validator function get called for a TextFormField?
AEvery time the user types a character in the field
BOnly when the field loses focus
COnly when the form's validate() method is called
DWhen the form is first built
Attempts:
2 left
💡 Hint
Think about when Flutter triggers validation checks.
🔧 Debug
advanced
2:00remaining
Debugging a Validator That Always Returns Null
A developer wrote this validator but it always returns null, even for invalid input: String? validator(String? value) { if (value!.isEmpty) { return 'Cannot be empty'; } if (value.length < 5) { 'Too short'; } return null; } What is the cause?
Flutter
String? validator(String? value) {
  if (value!.isEmpty) {
    return 'Cannot be empty';
  }
  if (value.length < 5) {
    'Too short';
  }
  return null;
}
AThe value is force unwrapped unsafely causing a crash
BThe second if block does not return the error string
CThe validator should return a bool, not String?
DThe validator is missing a final else block
Attempts:
2 left
💡 Hint
Look carefully at the second if block's code.
🧠 Conceptual
expert
2:00remaining
Combining Multiple Custom Validators in Flutter
You want to combine two custom validators for a TextFormField: one checks if input is not empty, the other checks if input is numeric. How should you combine them to show the first error found?
AReturn the first non-null error from calling each validator in order
BConcatenate both error messages and return them together
CCall both validators and return null only if both pass
DUse a try-catch block to catch errors from both validators
Attempts:
2 left
💡 Hint
Think about how Flutter expects a single error message or null.