Accessing FormField state

If we are using the TextFormField widget, things get simpler:

final _key = GlobalKey<FormFieldState<String>>();
...
TextFormField(
key: _key,
);

We can add a key to our TextFormField that later can be used to access the widget's current state through the key.currentState value, which will contain the updated value of the field.

The specialized type of key refers to the kind of data the input field works with. In the preceding example, this is String, because it is a TextField widget, so the key depends on the particular widget used.

The FormFieldState<String> class also provides other useful methods and properties to deal with FormField

  • validate() will call the widget's validator callback, which should check its current value and return an error message, or null if it's valid.
  • hasError and errorText result from previous validations using the preceding function. In material widgets, for example, this adds some small text near to the field, providing proper feedback to the user about the error.
  • save() will call the widget's onSaved callback. This is the action that happens when the input is done by the user (when it is being saved).
  • reset() will put the field in its initial state, with the initial value (if any), clearing validation errors as well.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.239.46