Reputation: 79
I'm writing an app where the login screen asks the user for various permissions and to login using Facebook.
Once all the permissions have been requested, the app moves to the next screen to ask further information and then returns to the calling screen to complete its tasks.
It seems that Navigator.push doesn't work as expected in async functions however.
void function() async {
<...do something 1...>
Navigator.pushNamed(context, screenName);
<...do something 2...>
}
Expected behaviour
would be that <...do something 1...>
would run, then Navigator would call the next screen, and once Navigator.pop(context);
was called by the second screen, the app would return to the first screen and execute <...do something 2...>
. This is the way it works in every other screen in the app.
Actual behaviour
<...do something 1...>
called, then <...do something 2...>
and then at some point the Navigator is triggered.
This is causing me to have to do all sorts to try to block the flow of the application through this, but it seems very peculiar behaviour.
Has anyone else experienced this, or can give me a way around it? This doesn't even involve the await
parts of the function, and to say it's driving me nuts is somewhat of an understatement.
Any help much appreciated!
Upvotes: 4
Views: 8605
Reputation: 554
I believe Navigator.pushNamed
returns a Future
. As such, perhaps if you just add the await
like so:
void function() async {
<...do something 1...>
await Navigator.pushNamed(context, screenName);
<...do something 2...>
}
It can solve the issue, although I did not try it.
Upvotes: 5
Reputation: 1192
You can use .then() function to achieve what you want, example, (Using email and password SignIn.)
onPressed: () {
authHandler.handleSignInEmail(emailController.text, passwordController.text)
.then((FirebaseUser user) {
Navigator.push(context, new MaterialPageRoute(builder: (context) => new HomePage()));
}).catchError((e) => print(e));
}
Upvotes: 1