11.7. Working with forms in Flutter: Storing and submitting form data

Listen in audio

When building apps with Flutter and Dart, one of the essential skills you need to acquire is the ability to work with forms. Forms are an integral part of any application as they allow you to collect user data, which can then be stored and sent to a server or used to interact with other parts of the application.

To start working with forms in Flutter, you need to understand some basic concepts. First, you should know that Flutter provides a class called Form which acts as a container for grouping various form fields. Each form field is represented by a FormField widget, which can be one of many different field types available, such as TextFormField, DropdownButtonFormField, and so on.

To create a form in Flutter, you must first create a Form widget and then add the desired FormField widgets to it. Each FormField widget requires a controller, which is an object that manages interactions between the user interface and the underlying data. The most commonly used controller is the TextEditingController, which can be used to control text fields.

Here is a simple example of how to create a form in Flutter:

Form(
  key: _formKey,
  child: Column(
    children: [
      TextFormField(
        controller: _nameController,
        decoration: InputDecoration(labelText: 'Name'),
        validator: (value) {
          if (value.isEmpty) {
            return 'Please enter your name';
          }
          return null;
        },
      ),
      TextFormField(
        controller: _emailController,
        decoration: InputDecoration(labelText: 'Email'),
        validator: (value) {
          if (value.isEmpty) {
            return 'Please enter your email';
          }
          return null;
        },
      ),
      RaisedButton(
        onPressed: _submitForm,
        child: Text('Send'),
      ),
    ],
  ),
)

Note that each TextFormField in the example above has a controller and a validation function. The validation function is used to verify that the value entered by the user is valid. If valid, the function returns null; otherwise, it returns an error message.

Now, let's see how you can store and submit form data. To store the data you can simply use the controller properties. For example, you can get the name field value using _nameController.text. To send the data, you can use any method you like, depending on your needs. For example, you can send the data to a server using an HTTP request, or you can store it locally using an SQLite database.

Here is an example of how you can store and submit form data:

void _submitForm() {
  if (_formKey.currentState.validate()) {
    String name = _nameController.text;
    String email = _emailController.text;

    print('Name: $name');
    print('Email: $email');

    // Here you can send the data to a server or store it locally
  }
}

Note that before storing and sending the data, you must validate the form by calling _formKey.currentState.validate(). This will trigger the validation functions for all form fields and return true if all fields are valid.

In summary, working with forms in Flutter involves creating a Form widget, adding FormField widgets to it, using controllers to manage interactions between the UI and the underlying data, validating the data, and finally storing and sending the data. By mastering these skills, you'll be well equipped to build robust, interactive apps with Flutter and Dart.

Now answer the exercise about the content:

What is the function of the `TextFormField` widget in Flutter?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Working with forms in Flutter: Working with complex forms

Next page of the Free Ebook:

156Working with forms in Flutter: Working with complex forms

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text