of static method

FormState of(
  1. BuildContext context
)

Returns the FormState of the closest Form widget which encloses the given context.

Typical usage is as follows:

FormState form = Form.of(context);
form.save();

If no Form ancestor is found, this will assert in debug mode, and throw an exception in release mode.

Calling this method will create a dependency on the closest Form in the context.

See also:

  • Form.maybeOf, which is similar to this method, but returns null if no Form ancestor is found.

Implementation

static FormState of(BuildContext context) {
  final FormState? formState = maybeOf(context);
  assert(() {
    if (formState == null) {
      throw FlutterError(
        'Form.of() was called with a context that does not contain a Form widget.\n'
        'No Form widget ancestor could be found starting from the context that '
        'was passed to Form.of(). This can happen because you are using a widget '
        'that looks for a Form ancestor, but no such ancestor exists.\n'
        'The context used was:\n'
        '  $context',
      );
    }
    return true;
  }());
  return formState!;
}