title: Flutter: How to populate form from a list data on click edit button and save it?
body: <p>I have a list which is hard coded at the beginning. When i make an entry in the form, the form data is saved to list. I want to get the data in my form of same index when i click on the update icon. The current screen is this.</p>
  <p><a href="https://i.stack.imgur.com/gFC1l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gFC1l.png" alt="enter image description here" /></a></p>
  <p>I want this output after clicking on Edit button. Is there any way I can do this?</p>
  <p><a href="https://i.stack.imgur.com/XxpOX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XxpOX.png" alt="enter image description here" /></a></p>
  <p>Here is my Code.</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:table/model.dart';
  
  class Episode5 extends StatefulWidget {
    @override
    _Episode5State createState() =&gt; _Episode5State();
  }
  
  class _Episode5State extends State&lt;Episode5&gt; {
    TextEditingController nameController = TextEditingController();
    TextEditingController emailController = TextEditingController();
  
    final form = GlobalKey&lt;FormState&gt;();
    static var _focusNode = new FocusNode();
    User user = User();
    List&lt;User&gt; userList = [
      User(name: &quot;a&quot;, email: &quot;a&quot;),
      User(name: &quot;d&quot;, email: &quot;b&quot;),
      User(name: &quot;c&quot;, email: &quot;c&quot;)
    ];
  
    @override
    Widget build(BuildContext context) {
      Widget bodyData() =&gt; DataTable(
            onSelectAll: (b) {},
            sortColumnIndex: 0,
            sortAscending: true,
            columns: &lt;DataColumn&gt;[
              DataColumn(
                  label: Text(&quot;Name&quot;),
                  numeric: false,
                  tooltip: &quot;To Display name&quot;),
              DataColumn(
                  label: Text(&quot;Email&quot;),
                  numeric: false,
                  tooltip: &quot;To Display Email&quot;),
              DataColumn(
                  label: Text(&quot;Update&quot;),
                  numeric: false,
                  tooltip: &quot;To Display Email&quot;),
            ],
            rows: userList
                .map(
                  (name) =&gt; DataRow(
                    cells: [
                      DataCell(
                        Text(name.name),
                      ),
                      DataCell(
                        Text(name.email),
                      ),
                      DataCell(
                        Icon(
                          Icons.edit,
                          color: Colors.black,
                        ),
                      ),
                    ],
                  ),
                )
                .toList(),
          );
  
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;Data add to List Table using Form&quot;),
        ),
        body: Container(
          child: Column(
            children: &lt;Widget&gt;[
              bodyData(),
              Padding(
                padding: EdgeInsets.all(10.0),
                child: Form(
                  key: form,
                  child: Container(
                    child: Column(
                      children: &lt;Widget&gt;[
                        TextFormField(
                          controller: nameController,
                          focusNode: _focusNode,
                          keyboardType: TextInputType.text,
                          autocorrect: false,
                          onSaved: (String value) {
                            user.name = value;
                          },
                          maxLines: 1,
                          validator: (value) {
                            if (value.isEmpty) {
                              return 'This field is required';
                            }
                            return null;
                          },
                          decoration: new InputDecoration(
                            labelText: 'Name',
                            hintText: 'Name',
                            labelStyle: new TextStyle(
                                decorationStyle: TextDecorationStyle.solid),
                          ),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        TextFormField(
                          controller: emailController,
                          keyboardType: TextInputType.text,
                          autocorrect: false,
                          maxLines: 1,
                          validator: (value) {
                            if (value.isEmpty) {
                              return 'This field is required';
                            }
                            return null;
                          },
                          onSaved: (String value) {
                            user.email = value;
                          },
                          decoration: new InputDecoration(
                              labelText: 'Email',
                              hintText: 'Email',
                              labelStyle: new TextStyle(
                                  decorationStyle: TextDecorationStyle.solid)),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        Column(
                          // crossAxisAlignment: CrossAxisAlignment.start,
                          children: &lt;Widget&gt;[
                            Center(
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: &lt;Widget&gt;[
                                  TextButton(
                                    child: Text(&quot;Add&quot;),
                                    onPressed: () {
                                      if (validate() == true) {
                                        form.currentState.save();
                                        addUserToList(
                                          user.name,
                                          user.email,
                                        );
                                        clearForm();
                                      }
                                    },
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }
  
    void addUserToList(name, email) {
      userList.add(User(name: name, email: email));
    }
  
    clearForm() {
      nameController.clear();
      emailController.clear();
    }
  
    bool validate() {
      var valid = form.currentState.validate();
      if (valid) form.currentState.save();
      return valid;
    }
  }
  </code></pre>