Flutter · One codebase, native feel
How do you build and validate forms in Flutter?
Short answer
Use Form with a GlobalKey<FormState>, TextFormField validators and controllers. Validate on submit or on interaction, dispose controllers, and mirror server errors into the form state.
A Form groups fields and exposes validate and save through a GlobalKey. Each TextFormField validator returns an error string or null. Validation is synchronous and local; asynchronous or server-side rules need explicit loading and error handling.
final formKey = GlobalKey<FormState>();
final emailController = TextEditingController();
Widget buildEmailField() {
return Form(
key: formKey,
child: TextFormField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
autofillHints: const [AutofillHints.email],
validator: (value) {
final text = value?.trim() ?? '';
if (!text.contains('@')) return 'Enter a valid email';
return null;
},
),
);
}
bool submit() {
return formKey.currentState?.validate() ?? false;
}- Dispose every TextEditingController and FocusNode in State.dispose
- Use autovalidateMode deliberately; validating on every keystroke can feel hostile
- Use inputFormatters for formatting and maxLength for hard limits
- Set textInputAction and focus traversal so keyboard navigation is sensible
- Expose errors through Semantics and keep labels visible for screen readers
Server errors and submission
Disable the submit button while a request is in flight and guard against double taps. Map field-level server errors back to the relevant controller or validator. Keep a form-level error area for problems that are not tied to one field. On success, clear sensitive controllers before leaving the screen.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you show a server-side validation error on a specific field?
- 2Why should controllers be disposed?
- 3What is the difference between validate and save?
- 4How do you prevent duplicate submissions?
Say it in the interview
I build forms with Form and TextFormField validators, keeping controllers in State and disposing them properly. Local rules validate immediately, but server rules come back as explicit field or form errors. I disable submission while a request is in flight and make sure the server validates everything again. Accessibility matters too, so labels and errors are exposed to screen readers.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Flutter mock interview and get scored feedback on depth, clarity and confidence.