Challenge - 5 Problems
Form Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you call validate() on a Form with a GlobalKey?
Consider a Flutter app with a Form widget linked to a GlobalKey. What is the result of calling
_formKey.currentState!.validate()?Flutter
final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); Form( key: _formKey, child: TextFormField( validator: (value) => value!.isEmpty ? 'Empty!' : null, ), ); // Later in code bool isValid = _formKey.currentState!.validate();
Attempts:
2 left
💡 Hint
Think about what validate() does in a form context.
✗ Incorrect
Calling validate() on the FormState runs all validators of the form fields and returns true only if all validators return null (meaning valid). It does not reset or submit the form.
❓ lifecycle
intermediate1:30remaining
What is the role of GlobalKey in managing Form state?
Why do we use a GlobalKey with a Form widget in Flutter?
Attempts:
2 left
💡 Hint
Think about how you access form methods like validate() or save().
✗ Incorrect
GlobalKey allows you to access the FormState object, which holds the current state of the Form, enabling you to call methods like validate() or save() from outside the Form widget.
📝 Syntax
advanced2:30remaining
Which code snippet correctly uses GlobalKey to validate a Form?
Select the code snippet that correctly creates a GlobalKey, assigns it to a Form, and validates the form on button press.
Attempts:
2 left
💡 Hint
Remember how to access FormState and call validate().
✗ Incorrect
Option C correctly declares GlobalKey, assigns it to the Form's key, and calls validate() on currentState. Other options have errors like missing type, wrong method calls, or missing null safety operators.
🔧 Debug
advanced2:00remaining
Why does _formKey.currentState return null sometimes?
In a Flutter app, you get a null error when calling
_formKey.currentState!.validate(). What is the most likely cause?Attempts:
2 left
💡 Hint
Think about widget lifecycle and when currentState is available.
✗ Incorrect
currentState is null if the Form widget is not yet inserted in the widget tree or has been removed. Calling validate() too early causes null errors.
🧠 Conceptual
expert3:00remaining
How does GlobalKey help preserve Form state during widget rebuilds?
In Flutter, why does using a GlobalKey help keep the form field values and validation state intact when the widget tree rebuilds?
Attempts:
2 left
💡 Hint
Think about how Flutter matches widgets and their states.
✗ Incorrect
GlobalKey lets Flutter know which widget instance corresponds to which state object, so during rebuilds, the state is preserved and reused rather than reset.