Explain the validator property in TextFormField and what triggers error display
Tests Form validation lifecycle knowledge. Strong answer: validator returns String? error or null; formKey.currentState.validate() triggers all fields; returned string renders as inline error.
WHAT THIS TESTS: This question probes whether you understand the relationship between Form state, TextFormField, and the validation lifecycle in Flutter. Interviewers want to see that you know validation is declarative, centralized through the Form widget, and distinct from change handling or saving. They also check if you understand the difference between controlled form fields and raw input widgets.
A GOOD ANSWER COVERS: First, define validator as a callback with signature String? Function(String?) that receives the field's current value and returns an error string if invalid or null if valid. Second, explain that validation does not run automatically on every keystroke; it fires when formKey.currentState.validate() is invoked, which walks the form tree and calls every registered field's validator in sequence. Third, describe the UI feedback mechanism: the returned string is rendered as red error text in the InputDecorator below the text line, the border color shifts to error red, and the helper text is replaced. Fourth, mention that TextFormField must live inside a Form widget for validate() to reach it, because the Form maintains the list of registered form fields via their FormFieldState. Fifth, note that validation is synchronous by default; async checks require a custom pattern.
COMMON WRONG ANSWERS: Believing that onChanged, onEditingComplete, or onSaved triggers the error display. Implementing validation logic inside setState or onChanged rather than the validator callback. Using a plain TextField and manually managing an errorText controller while missing the built-in Form integration. Returning a boolean from validator instead of String?. Assuming validation runs automatically without calling validate().
LIKELY FOLLOW-UPS: How would you validate multiple fields together, like confirming a password matches? How do you trigger validation on every keystroke instead of on submit? What is the difference between validator and onSaved, and when does each run? How would you debounce async validation, such as checking username availability against a backend? How do you reset or clear validation state programmatically?
ONE CONCRETE EXAMPLE: To ensure a field is not empty, you write validator: (value) { if (value == null || value.isEmpty) { return 'Please enter some text'; } return null; }. Then wrap the TextFormField in a Form with a GlobalKey<FormState>. Inside an ElevatedButton's onPressed, call if (formKey.currentState!.validate()) { proceed(); }. When the button is pressed and the field is empty, the red error text appears instantly below the input; if the user types text and presses the button again, validate re-runs and the error disappears because the callback now returns null.
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.