Home > Software design >  How setState() redraw the new state in Flutter
How setState() redraw the new state in Flutter

Time:12-13

How setState() function redraw the new state after quick changes in statefull management in Flutter

It easily redraw the new state but how this concept work? I am beginner in Flutter

CodePudding user response:

According to Flutter Documentation,

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 you want to update the state of a widget then you need to use a setState function. Though you must have a Stateful Widget in order to use it as a widget's state can't be changed in a Stateless Widget. You can read more about it on Flutter Website

CodePudding user response:

Statefull widgets are obvious in flutter, these widgets are immutable but their states are mutable, they can change,

https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html

When you finally run your APP, you create a widget tree and for that widget tree flutter creates a corresponding element tree,

So when you call setState, the flutter framework checks for changes in widget tree and and updates corresponding element tree,

****Flutter framework is so smart that without building whole element tree from the scratch, it just updates it as per changes in widget tree.

***To get more performance benefit keep the state deeper inside the widget tree so the parents remain unaffected while flutter framework update the element tree.

  • Related