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 this Flutter form is submitted?
Consider this Flutter code snippet for a form with a submit button. What will be the output behavior when the user taps the submit button without entering any text?
Flutter
final _formKey = GlobalKey<FormState>(); Form( key: _formKey, child: Column( children: [ TextFormField( validator: (value) { if (value == null || value.isEmpty) { return 'Please enter some text'; } return null; }, ), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { print('Form submitted'); } }, child: Text('Submit'), ), ], ), )
Attempts:
2 left
💡 Hint
Think about what the validator function does when the input is empty.
✗ Incorrect
The validator checks if the text field is empty and returns an error string if so. The form's validate() method returns false, so the print statement is not executed and the error message appears below the field.
🧠 Conceptual
intermediate1:30remaining
What is the role of GlobalKey in Flutter forms?
Why do we use a GlobalKey when creating a form in Flutter?
Attempts:
2 left
💡 Hint
Think about how you call validate() on the form state.
✗ Incorrect
GlobalKey lets us access the form's current state anywhere in the widget tree, so we can call methods like validate() or save() on the form.
📝 Syntax
advanced2:00remaining
Which option correctly saves form data on submission?
Given a Flutter form with a TextFormField, which code snippet correctly saves the input value to a variable when the form is submitted?
Flutter
String? _name; final _formKey = GlobalKey<FormState>(); Form( key: _formKey, child: Column( children: [ TextFormField( onSaved: (value) => _name = value, validator: (value) => value == null || value.isEmpty ? 'Enter name' : null, ), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { // What goes here? } }, child: Text('Submit'), ), ], ), )
Attempts:
2 left
💡 Hint
Which method triggers onSaved callbacks?
✗ Incorrect
Calling save() on the form state triggers all onSaved callbacks, storing the input values. validate() only checks inputs, reset() clears them, dispose() frees resources.
❓ lifecycle
advanced2:00remaining
What happens if you call setState inside onSaved during form submission?
In a Flutter form, if you call setState() inside the onSaved callback of a TextFormField, what is the expected behavior?
Flutter
String? _email; TextFormField( onSaved: (value) { setState(() { _email = value; }); }, validator: (value) => value == null || value.isEmpty ? 'Enter email' : null, ),
Attempts:
2 left
💡 Hint
setState schedules a rebuild after the current frame.
✗ Incorrect
Calling setState inside onSaved is valid and causes the widget to rebuild with updated state after form submission.
🔧 Debug
expert2:30remaining
Why does this Flutter form not show validation errors?
This Flutter form does not show any validation error messages even when the input is empty. What is the cause?
Flutter
final _formKey = GlobalKey<FormState>(); Form( child: Column( children: [ TextFormField( validator: (value) => value == null || value.isEmpty ? 'Required' : null, ), ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { print('Valid'); } }, child: Text('Submit'), ), ], ), )
Attempts:
2 left
💡 Hint
Check how the form key is used.
✗ Incorrect
Without assigning the GlobalKey to the Form's key property, calling validate() on _formKey.currentState causes a null error or does nothing, so no validation messages appear.