You don't have to wait for the build, you should build something, to show to the user that the app is loading something (or a blank screen), and then rebuild when the funcion ends.
You could have a Widget variable that is set to a default when you create the widget, say with a CircularProgressIndicator
, then change it with setState, something like this:
class YourWidgetState extends State<YourWidget> {
Widget _body = CircularProgressIndicator(); // Default Body
@override
void initState(){
_gotoHomeScreen();
}
@override
Widget build(BuildContext context){
return _body;
}
Widget _gotoHomeScreen() {
AuthService.getuserPrefEmail().then((email){
AuthService.email = email;
if (email == null) {
setState(() => _body = LoginScreen());
} else {
AuthService.uid = email;
setState(() => _body = HomeMenuScreen());
}
});
}
}
Another way would be to use a variable to inform you about the loading situation, like bool finishedLoading
, and call setState
to change the value when it's done, using the email
variable you set to know when the user is logged in, and do a conditional build, like this:
bool loading = true;
@override
Widget build(BuildContext context){
if(loading) return CircularProgressIndicator();
if(AuthService.email == null)
return LoginScreen();
else
return HomeMenuScreen();
}
Widget _gotoHomeScreen() {
AuthService.getuserPrefEmail().then((email){
AuthService.email = email;
if (email != null) {
AuthService.uid = email;
}
setState((){ loading = false; });
});
}