As newbie in flutter it's very confusing for me when use setState
in Flutter
application. In below code boolean searching
and var resBody
used inside setState
. My question is why only searching
and resBody
inside setState? Why not others variable?
var resBody; bool searching = false,api_no_limit = false; String user = null; Future _getUser(String text) async{ setState(() { searching = true; }); user = text; _textController.clear(); String url = "https://api.github.com/users/"+text; var res = await http .get(Uri.encodeFull(url), headers: {"Accept": "application/json"}); setState(() { resBody = json.decode(res.body); }); }
Whenever you change the internal state of a State object, make the change in a function that you pass to setState: setState(() { _myState = newValue; }); The provided callback is immediately called synchronously.
setState is the Flutter way to issue a rebuild of the current widget and its descendants. During a rebuild, the most recent variable values will be used to create the user interface. Let's say, a user toggles a switch from on to off.
When you change the state of a Stateful Widget, use setState() to cause a rebuild of the widget and its descendants. You don't need to call setState() in the constructor or initState() of the widget, because build() will be run afterward anyway. Also don't call setState() in synchronous code inside build().
ValueNotifier requires a bit more code than setState . But it can be used to remember the state, by placing a Provider where appropriate in the widget tree.
One of the keys when writing Flutter code is to know when to call the setState method. As a quick answer, the Flutter docs state: Whenever you change the internal state of a State object, make the change in a function that you pass to setState.
What is a State Object in flutter? setState is called inside a State class. Let's understand this in detail. State is simply the information of a StatefulWidget. Every StatefulWidget has a State Object. This State Object keeps a track of the variables and functions that we define inside a StatefulWidget.
Widget: Any UI component on the screen is called a Widget in flutter. A Widget can have its own Widgets, like a tree structure. StatefulWidget: A Widget that can change dynamically. Generally used when we want to modify something on the screen's UI. What is a State Object in flutter? setState is called inside a State class.
Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree ... (this) causes the framework to schedule a build for this State object ...
According to the docs:
Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.
So if the state of the widget changes you have to call setState
to trigger a rebuild of the view and see immediatly the changes implied by the new state.
Anyhow the below snippets are equivalent.
first case (directly form flutter create <myproject>
):
class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void _incrementCounter() { setState(() { // This call to setState tells the Flutter framework that something has // changed in this State, which causes it to rerun the build method below // so that the display can reflect the updated values. If we changed // _counter without calling setState(), then the build method would not be // called again, and so nothing would appear to happen. _counter++; }); }
second case:
class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void _incrementCounter() { _counter++; setState(() {}); }
What I don't know is the reason why and if the first case is the conventional way to use setState
, I would say because of readability of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With