So I have code that basically like this:
int data;
void function() {
print("First: $data");
if (data == null) {
this.setState(() => data = 1);
}
print("Second: $data");
}
The log will be:
First: null
Second: null
But if I do it like this:
int data;
void function() {
print("First: $data");
if (data == null) {
data = 1;
this.setState(() => data = 1);
}
print("Second: $data");
}
The result will be:
First: null
Second: 1
Why it's like this? I thought for a long time the calling of callback inside setState is synchronous? If it isn't then is it better if I do this instead:
int data;
void function() {
print("First: $data");
if (data == null) {
data = 1;
this.setState(() {}); // nothing in the callback, set state is just marking UI needs to be updated
}
print("Second: $data");
}