title: Flutter: keep data in textfield after setstate
body: <ol>
  <li>I have an array of textfields.</li>
  <li>A button which adds a new textfield to the array.</li>
  <li>If you add data to the textfield and add a new element by clicking the add button, the data added is gone.</li>
  <li>The button function has a setstate in there to see the new elements to the array.</li>
  <li>How can I add new elements to the array keeping the old data in the textfields?</li>
  </ol>
  <p>start_workout.dart</p>
  <pre><code>    import 'package:flutter/material.dart';
  import 'package:fultter_ultralifestyle/src/models/models.dart' show SetModel;
  import 'package:fultter_ultralifestyle/src/presentation/widgets/dynamic_widget.dart';
  
  class WorkoutStartScreen extends StatefulWidget {
    static const String routeName = &quot;workoutStart&quot;;
    @override
    _WorkoutStartScreenState createState() =&gt; _WorkoutStartScreenState();
  }
  
  class _WorkoutStartScreenState extends State&lt;WorkoutStartScreen&gt; {
    final List&lt;SetModel&gt; sets = [
    ];
  
    void _addSet() {
      final id = sets.length;
      sets.add(SetModel(id: id, pounds: 0, reps: 0));
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;Workout tracker&quot;),
          centerTitle: true,
        ),
        body: Container(
          child: Column(
            children: &lt;Widget&gt;[
              Expanded(
                child: ListView.builder(
                    itemCount: sets.length,
                    itemBuilder: (BuildContext context, int index) {
                      return DynamicWidget(
                        set: sets[index],
                        pos: index +1,
                        delete: () {
                          sets.removeAt(index);
                          setState(() {});
                        },
                      );
                    }),
              )
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () =&gt; _addSet(),
          child: Icon(Icons.add),
        ),
      );
    }
  }
  </code></pre>
  <p>dynamic_widget.dart</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  import 'package:fultter_ultralifestyle/src/models/models.dart';
  import 'package:fultter_ultralifestyle/src/presentation/widgets/text_widget.dart';
  
  class DynamicWidget extends StatelessWidget {
    final TextEditingController poundsController = TextEditingController();
    final TextEditingController repsController = TextEditingController();
  
    final SetModel set;
    final int pos;
    Function delete;
  
    DynamicWidget({
      Key key,
      @required this.set,
      @required this.pos,
      @required this.delete,
    }) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      final Size size = MediaQuery.of(context).size;
  
      return Container(
        child: Column(
          children: &lt;Widget&gt;[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: &lt;Widget&gt;[
                Container(
                  child: text(
                    caption: &quot;SET $pos&quot;,
                  ),
                ),
                SizedBox(width: 20.0),
                Container(
                  width: size.width / 4,
                  child: Column(
                    children: &lt;Widget&gt;[
                      TextField(
                        controller: poundsController,
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          WhitelistingTextInputFormatter.digitsOnly,
                        ],
                      ),
                      SizedBox(height: 10),
                      text(caption: &quot;pounds&quot;.toUpperCase()),
                    ],
                  ),
                ),
                SizedBox(
                  width: 20,
                ),
                text(caption: &quot;X&quot;),
                SizedBox(
                  width: 20,
                ),
                Container(
                  width: size.width / 4,
                  child: Column(
                    children: &lt;Widget&gt;[
                      TextField(
                        controller: repsController,
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          WhitelistingTextInputFormatter.digitsOnly
                        ],
                      ),
                      SizedBox(height: 10),
                      text(caption: &quot;reps&quot;.toUpperCase()),
                    ],
                  ),
                ),
                SizedBox(width: 5.0),
                IconButton(
                  icon: Icon(Icons.delete_forever),
                  onPressed: delete,
                )
              ],
            ),
          ],
        ),
      );
    }
  }
  </code></pre>