I need to set the value of a TextEditingController
during the widget build method.
The problem is that every time the widget rebuilds, the _controller value resets to to it’s original state and the new value disappears:
final TextEditingController _controller = new TextEditingController();
Widget _build(BuildContext context) {
myString = "Value only available in widget build";
new TextField(
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
controller: _controller..text = myString,
),
}
In the above example myString
is declared during widget build. Unfortunately, it cannot be declared duing initState
, hence my problem.
How can I set the _controller value only during the first build and prevent it from resetting to it’s original value after the widget rebuilds itself?
Any suggestions?
2
Answers
From what you are saying, you don’t need to use StoreConnector for widget you don’t want to update when updating your store. You could definitely do something like this:
Also, if you want to do it with
StoreConnector<>
you can do:You can set text controller value into initState() of your page so it will be prevent it to reset after rebuild