I am new in Flutter. I want to change the value using setState
but it doesn't work.
I would appreciate any help.
CodePudding user response:
Make sure the value that you want to change is outside the Widget.
CodePudding user response:
According to docs setState()
defination is
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.
For example:
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter ;
});
}
so the abovesetState()
is called telling the flutter framwork that something has changed , which causes the rerun the build method and the updated part is reflected on the screen.If we change the _counter
without calling setstate() than the changes wont be reflected.