FormState class

Reactive form with validation and submit handling.

Constructors

FormState(Map<String, FieldDef> fields, {List<FormValidator> validators = const [], List<AsyncFormValidator> asyncValidators = const [], AutovalidateMode autovalidateMode = AutovalidateMode.disabled})

Properties

autovalidateMode AutovalidateMode
Current form autovalidate mode.
no setter
errors Map<String, String?>
All field errors as a map.
no setter
fieldNames List<String>
Immutable list of field names.
no setter
firstErrorFieldName String?
First field name in order that currently has an error.
no setter
formError String?
Cross-field validation error.
no setter
hasErrors bool
Whether any field has an error.
no setter
hashCode int
The hash code for this object.
no setterinherited
isDirty bool
Whether any field is dirty.
no setter
isTouched bool
Whether any field is touched.
no setter
isValid bool
Whether the form currently has no field/form errors.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
submitCount int
Number of submit attempts.
no setter
submitError String?
Last submit error.
no setter
submitted bool
Whether at least one submit attempt happened.
no setter
submitting bool
Whether form is currently submitting.
no setter
validating bool
Whether any field is currently async validating.
no setter
values Map<String, dynamic>
All field values as a map.
no setter

Methods

bindTextField(String name, {bool validateOnChange = false, bool validateOnBlur = true, bool moveFocusOnSubmit = true, bool scrollOnFocus = false, FutureOr<void> onDone()?}) TextFieldBinding
Create binding callbacks for a text input component.
cancelAsyncValidation() → void
Cancel in-flight async validations.
clearErrors() → void
Clear all field and form-level errors.
clearSubmitError() → void
Clear submit error.
containsField(String name) bool
Whether a field exists in this form.
focusField(String name, {bool scroll = false}) bool
Request focus for a field.
focusNext(String currentName, {bool scroll = false}) bool
Focus next field in order.
focusPrevious(String currentName, {bool scroll = false}) bool
Focus previous field in order.
nextFieldName(String currentName) String?
Get the next field in definition order.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
previousFieldName(String currentName) String?
Get the previous field in definition order.
reset({bool keepValues = false}) → void
Reset all fields.
save() Map<String, dynamic>
Save all fields and return current values snapshot.
setErrors(Map<String, String?> errors, {String? formError}) → void
Set multiple field errors and optional form-level error.
setFieldError(String name, String? error) → void
Set or clear a field error.
setFocusHandler(void handler(String name)?) → void
Register an integration handler to focus a field in UI.
setScrollHandler(void handler(String name)?) → void
Register an integration handler to scroll a field into view in UI.
setSubmitError(String? message) → void
Set or clear the submit error.
submit(Future<void> handler(Map<String, dynamic> values), {bool touch = true, bool allowConcurrent = false, bool runAsyncValidators = true, bool focusFirstError = false, bool scrollToFirstError = false}) Future<bool>
Submit the form with a handler.
toString() String
A string representation of this object.
inherited
touchAll() → void
Mark all fields as touched.
validate({bool touch = true}) bool
Validate all synchronous validators.
validateAndFocusFirstError({bool touch = true, bool scroll = true}) bool
Validate and focus/scroll the first invalid field.
validateAndScrollToFirstError({bool touch = true}) bool
Validate and request scrolling to first error field.
validateAsync({bool touch = true}) Future<bool>
Validate synchronous and asynchronous validators.
validateAsyncAndFocusFirstError({bool touch = true, bool scroll = true}) Future<bool>
Async validate and focus/scroll the first invalid field.
validateAsyncAndScrollToFirstError({bool touch = true}) Future<bool>
Validate and request scroll to first error field (async).
validateField(String name, {bool touch = true}) bool
Validate a specific field synchronously.
validateFieldAsync(String name, {bool touch = true}) Future<bool>
Validate a specific field including async validators.

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](String name) FormField
Get a field by name.