Build and validate a login form with Form and GlobalKey
This tests Flutter's declarative form validation. A strong answer covers wrapping fields in a Form, using a GlobalKey<FormState>, calling validate on submit, and acting only on true.
WHAT THIS TESTS: This question evaluates whether you understand Flutter's built-in form validation pattern and how to coordinate state across multiple input widgets without manually tracking each field. The interviewer wants to see that you know the difference between a TextField and a TextFormField, that you understand the Form widget acts as a container that aggregates validation state, and that you can use a GlobalKey to imperatively trigger validation from a button that lives outside the individual fields.
A GOOD ANSWER COVERS: A strong response should hit four things in order. First, create a GlobalKey<FormState> at the widget level so the Form can be referenced from the button. Second, wrap the email and password inputs in a Form widget and pass that key to the Form key parameter. Third, use TextFormField widgets rather than plain TextFields, and supply a validator function to each one that returns a descriptive error string when input is invalid or null when it is valid. Fourth, in the Submit button onPressed handler, call key.currentState.validate(), which returns true only if every descendant validator returns null, and only then proceed to authenticate. You should also mention that calling validate() automatically rebuilds the fields to display error text.
COMMON WRONG ANSWERS: A red flag is describing a manual validation scheme where you store separate TextEditingControllers for email and password, run regex checks inside setState, and conditionally show error text via ternary operators in the build method. Another red flag is forgetting that GlobalKey<FormState> is the correct type, or suggesting GlobalKey<Form> which is incorrect. Some candidates also suggest validating inside onChanged, which triggers feedback on every keystroke rather than at submission time. Finally, saying you would use a StatefulWidget but then never explaining how the Form state is actually accessed shows a shallow understanding.
LIKELY FOLLOW-UPS: The interviewer may ask how you would reset the form after a successful login, which is done by calling key.currentState.reset(). They might ask how to save values without manually reading controllers, which is done by calling key.currentState.save() and implementing onSaved on each TextFormField. They could also ask about autovalidate modes, keyboard types, or how to handle focus traversal between the email and password fields using FocusNode and TextInputAction.next.
ONE CONCRETE EXAMPLE: Imagine a login screen with an email TextFormField whose validator checks for an empty string and a valid email pattern, returning Please enter a valid email when invalid. The password field below it has a validator that enforces a minimum length of eight characters. Both sit inside a Form with a GlobalKey<FormState> named formKey. The Submit button is an ElevatedButton whose onPressed checks whether formKey.currentState.validate() returns true before calling the login method. When the user taps Submit with empty fields, both validators run, both fields rebuild with red error text, and login is skipped. When they fix the inputs and tap again, validate() returns true and the network request fires.
Read the original → docs.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.