0
0
Fluttermobile~20 mins

Form submission in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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'),
      ),
    ],
  ),
)
AThe form shows a validation error message below the text field and does not print anything.
BThe form prints 'Form submitted' to the console even if the text field is empty.
CThe app crashes with a null pointer exception because the form key is not initialized.
DThe submit button is disabled and cannot be tapped.
Attempts:
2 left
💡 Hint
Think about what the validator function does when the input is empty.
🧠 Conceptual
intermediate
1:30remaining
What is the role of GlobalKey in Flutter forms?
Why do we use a GlobalKey when creating a form in Flutter?
AIt disables the form fields until the key is set.
BIt styles the form fields with a global theme.
CIt automatically submits the form when the user presses enter.
DIt uniquely identifies the form widget and allows us to access its state for validation and saving.
Attempts:
2 left
💡 Hint
Think about how you call validate() on the form state.
📝 Syntax
advanced
2: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'),
      ),
    ],
  ),
)
A_formKey.currentState!.save(); print('Name saved: $_name');
B_formKey.currentState!.validate(); print('Name saved: $_name');
C_formKey.currentState!.reset(); print('Name saved: $_name');
D_formKey.currentState!.dispose(); print('Name saved: $_name');
Attempts:
2 left
💡 Hint
Which method triggers onSaved callbacks?
lifecycle
advanced
2: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,
),
AThe app freezes because setState is called during build.
BThe UI updates immediately with the new _email value after form submission.
CThe setState call is ignored and the UI does not update.
DCalling setState inside onSaved causes a runtime error.
Attempts:
2 left
💡 Hint
setState schedules a rebuild after the current frame.
🔧 Debug
expert
2: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'),
      ),
    ],
  ),
)
AThe validator function is incorrect and always returns null.
BThe TextFormField is missing a controller, so validation is skipped.
CThe Form widget is missing the key property, so validate() cannot find the form state.
DThe ElevatedButton onPressed callback is not called because the button is disabled.
Attempts:
2 left
💡 Hint
Check how the form key is used.