I have a splash screen, where I call an api, and get a string response. I save the response as alertMessage. I want to pass this string (alertMessage) to the next screen.
I make the api call in initState, like this:
@override
void initState() {
PreLoadApi().fetchPreLoad().then((value){
int numberOfEntries = GlobalCall.outcomes.categoryData[0].categoryTextAndID.length;
for (int i = 0; i < numberOfEntries; i++) {
String entry = GlobalCall.outcomes.categoryData[0].categoryTextAndID[i].categoryTextDetail;;
print('2124: From preload, value $i: $entry');
if(i==0){
// Here I set alertMessage
alertMessage = entry;
}
print('2124: AlertMessage from initState: $alertMessage');
}
});
Now, my build method is like this:
@override
Widget build(BuildContext context) {
print('2124: AlertMessage from build: $alertMessage');
ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false);
return new SplashScreen(
seconds: 3,
navigateAfterSeconds: token != '' ? new MenuScreen(alertMessage: alertMessage): new LoginScreen(alertMessage: alertMessage),
//title: new Text('Welcome In SplashScreen'),
image: new Image.asset('assets/logo/ic_logo.png'),
backgroundColor: Colors.white,
styleTextUnderTheLoader: new TextStyle(),
photoSize: MediaQuery.of(context).size.width * 0.5,
loaderColor: Colors.blue,
);
}
}
Now, I understand the problem, but don't know the solution. The build method is called before the api result comes in. So when I call return new SplashScreen, the value of alertMessage is still null. Even though the splash screen stands for 3 seconds, but when its first called he alertMessage value is null. How can I make it that the value alertMessage will be updated once the api result comes in?
I hope its clear.
Thanks