Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Working with forms in Flutter: Storing and submitting form data

Capítulo 155

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The TextFormField widget in Flutter is used to create an input field that collects user data such as text. It is a specific type of FormField that supports validation and is commonly used in forms to gather information from the user.

Next chapter

Working with forms in Flutter: Working with complex forms

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.