FormController class

Controller for managing form state, validation, and submission.

The FormController coordinates all form field interactions, maintaining a centralized registry of field values and validation states. It provides programmatic access to form data collection, validation triggering, and submission handling.

The controller automatically manages the lifecycle of form fields as they register and unregister, tracking their values and validation results. It supports both synchronous and asynchronous validation, cross-field validation dependencies, and comprehensive error state management.

Example:

final controller = FormController();

// Listen to form state changes
controller.addListener(() {
  print('Form validity: ${controller.isValid}');
  print('Form values: ${controller.values}');
});

// Submit the form
await controller.submit();

// Access specific field values
final emailValue = controller.getValue(emailKey);
Inheritance

Constructors

FormController.new()

Properties

errors Map<FormKey, ValidationResult>
A map of all current validation errors keyed by their FormKey.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
validities Map<FormKey, FutureOr<ValidationResult?>>
A map of all current validation results keyed by their FormKey.
no setter
values Map<FormKey, Object?>
A map of all current form field values keyed by their FormKey.
no setter

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
attach(BuildContext context, FormFieldHandle handle, Object? value, Validator? validator, [bool forceRevalidate = false]) FutureOr<ValidationResult?>
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
override
getError(FormKey key) FutureOr<ValidationResult?>?
Retrieves the validation result for a specific form field.
getSyncError(FormKey key) ValidationResult?
Retrieves the synchronous validation result for a specific form field.
getValue<T>(FormKey<T> key) → T?
hasValue(FormKey key) bool
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
revalidate(BuildContext context, FormValidationMode state) → void
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited