The thing I am doing is calling setState() method and then updating the state after a delay of 5 seconds. Even then, i am seeing the State getting updated.
setState(() {
print("callback");
});
sleep(const Duration(seconds: 5));
_randomNumber = Random().nextInt(100);
print("Number : $_randomNumber");
As you can see after the delay, _randomNumber is changing & I have set this _randomNumber value to a Text Widget. After 10 seconds, text is getting updated with the new _randomNumber. Then what is the use of calling setState(() {}) & wrapping the state changes only inside setState() ?
setState
is the way to force an update immediately. It is your chance to control the timing. It is not the only way that the tree can get rebuild. Any number of other events can result in another call to your build function. But then you are not in control. It will get updated as a side-effect of something else happening.