setState does not update TextFormField when use initialValue
Solution 1:
If you have a reactive data source, aka data that can change based on either network updates or other data, one hack that worked for me was to use a Key
.
By making a Key
of the reactive data (toString()
), the form field will change every time the Key
changes.
So in this case you could do:
TextFormField(
key: Key(_total.toString()), // <- Magic!
initialValue: _total.toString(),
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Total',
),
),