TextFormField value not updating as intended in flutter. Here is an example of working code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => new _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String s1 = ""; String s2 = ""; String s3 = ""; String s4 = ""; String s5 = ""; final TextEditingController c1 = new TextEditingController(); final TextEditingController c2 = new TextEditingController(); final TextEditingController c3 = new TextEditingController(); final TextEditingController c4 = new TextEditingController(); final TextEditingController c5 = new TextEditingController(); @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(title: new Text("Example"),), body: new ListView( children: <Widget>[ new Padding(padding: new EdgeInsets.all(70.0),), new TextField( controller: c1, onChanged: (String text) { s1 = text; }, ), new Padding(padding: new EdgeInsets.all(70.0),), new TextField( controller: c2, onChanged: (String text) { s2 = text; }, ), new Padding(padding: new EdgeInsets.all(70.0),), new TextField( controller: c3, onChanged: (String text) { s3 = text; }, ), new Padding(padding: new EdgeInsets.all(70.0),), new TextField( controller: c4, onChanged: (String text) { s4 = text; }, ), new Padding(padding: new EdgeInsets.all(70.0),), new TextField( controller: c5, onChanged: (String text) { s5 = text; }, ), ], ) ); } } |
You need to add a TextEditingController with your TextField. Listview removes invisible widgets because rendering a ton of widgets that are not being displayed would make it perform worse!
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.