Reputation: 97
This might be not technical question,however, I read that whenever you want to change StatefullWidget state you need to call setState() function. i gave a test for EditText() widget and use the EditingTextController as widget controller, i called the controller.text = "some text"
from user-defined function without calling setState()
function and the value of the EditText() changed to be some text !
i would like to know how this done? i read the documentation and it says EditingTextController
will change the value of the widget but we already know that in order to change
State Full Widget state you need to call setState()
Upvotes: 1
Views: 1076
Reputation: 3555
Kind of this is true. But to change the state in Flutter you have other options, and setState(() {});
is only one of them. Another option is to create Notifier -> listener
, then set the listener to listen for the Notifier, like this TextFormField(controller: _controller,...)
so any update on the Notifier(controller), will notify the listener(TextFormField) to update itself.
e.g:
//...
final _controller = TextEditingController();
//...
child: TextFormField(
controller: _controller,
decoration: InputDecoration(border: OutlineInputBorder()),
),
in this example:
and once you call this _controller.text=..
the _controller (which is consider as ChangeNotifier see this.) "will notify all the listeners of this TextEditingController that they need to update", so it will give you same result as using setState(() {});
or Stream/StreamBuilder
or Future/FutureBuilder
...etc.
Upvotes: 1