Validating user input

Validating user input is one of the main functions of the Form widget. In order to make the data input entered by the user consistent, it is fundamental to check it, as the user probably does not know all the allowed values.

The Form widget, combined with FormField instances, helps the developer to show an appropriate error message if some input values need to be corrected before saving the form data through its save() function.

We already have seen, in the previous Form examples, how to validate the Form field values:

  1. Create a Form widget with a FormField on it.
  2. Define the validation logic on each FormField validator property:
  TextFormField(
validator: (String value) {
return value.isEmpty ? "The value cannot be empty" : null;
},
)
  1. Call validate() on FormState by using its key, or the Form.of method discussed previously. This will call each child FormField validate() method, and where the validation is successful, it will return true, and false otherwise.
  2. validate() returns a bool so we can manipulate its result and do our logic based on it.

..................Content has been hidden....................

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