A Form groups editable fields and coordinates validation. A validator returns a message when a value is invalid and null when it satisfies the local rule.
Validate the intended requirement
For this example, a project name must contain at least three non-whitespace characters. A production form may also need server-side uniqueness and authorization checks; passing a local validator does not prove the backend will accept the value.
Worked example
This is a complete example. Open it with Run example, then use Run inside DartPad.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ProjectForm()));
class ProjectForm extends StatefulWidget {
const ProjectForm({super.key});
@override
State<ProjectForm> createState() => _ProjectFormState();
}
class _ProjectFormState extends State<ProjectForm> {
final formKey = GlobalKey<FormState>();
String name = '';
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Create a project')),
body: Padding(padding: const EdgeInsets.all(24), child: Form(key: formKey,
child: Column(children: [
TextFormField(decoration: const InputDecoration(labelText: 'Project name'),
validator: (value) => (value?.trim().length ?? 0) < 3
? 'Enter at least three characters.' : null,
onSaved: (value) => name = value!.trim()),
const SizedBox(height: 20),
FilledButton(onPressed: () {
if (!formKey.currentState!.validate()) return;
formKey.currentState!.save();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Created $name')));
}, child: const Text('Create project')),
]))),
);
}Read the result
The key remains stable for the State object. Validation runs before save. The non-null assertions are scoped to a form that exists and a field whose validator has accepted a value; do not use assertions as a substitute for validation elsewhere.
Follow one invalid and one valid submission
With an empty input, the validator returns a message. validate reports that the form is invalid, so the handler returns before save or feedback. The user remains on the form with an actionable description of what to change.
With a valid project name, the validators return null. The handler then calls save, which runs the field’s onSaved callback and copies the accepted trimmed value into the feature’s result. Keeping those phases in order prevents a handler from treating an invalid editing value as saved data.
The key is created once for the State, so a rebuild does not replace the form identity unnecessarily. Its currentState access is appropriate only while the Form associated with that key is actually in the tree.
For a real remote save, passing client validation is the start of the request, not proof of acceptance. Add an in-flight state, reject duplicate submissions and represent a server failure using async UI states . Validate authorization and domain constraints on the backend independently of this client form.
Practice and verification
Test empty input, two letters, spaces and a valid project name. Add an asynchronous save phase that disables repeated submission and shows a server error inline when saving fails.