Since flutter calls the build method many times in different condition, to avoid getting the data many times, I initialize the data in initState
.
I want
I moved my code to my build method from initState and it worked
class _TestState extends State {
Data data;
bool dataReady = false;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
getData(context).then((Data data) async {
setState(() {
dataReady= true;
});
});
if (dataReady) {
return createMainContent(context);
} else {
return new Container();
}
}
}