行动后生成Flutter路线? [英] Generating Flutter route after action?
问题描述
成功登录后,我试图自动返回到菜单屏幕,但无法生成Widget构建和/或上下文。我正在使用无状态小组件进行此操作。这是我的代码。我想在最后一个打印语句之后调用路由...
I'm trying to automatically route back to my menu screen after a successful login and I'm having trouble generating a Widget build and/or context. I'm doing this in a Stateless Widget. Here is my code. I would like to call route after the last print statement...
Future<Null> _handleSignIn() async {
try {
await _googleSignIn.disconnect();
GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
print(googleAuth.idToken);
await FirebaseAuth.instance.signInWithGoogle(idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
} catch (error) {
print(error);
}
print("Let's Do This");
print(_googleSignIn.currentUser.displayName);
print(FirebaseAuth.instance.currentUser);
}
推荐答案
根据您的理解问题,基本上您需要设置登录按钮,以将用户重定向到成功登录后希望到达的任何位置。
From what I understand from your question you basically need to setup your sign in button to redirect the users to wherever you want them to be after a successful sign in.
您可以按以下方式设置onPressed呼叫,那么您应该可以从这里:
You can setup your onPressed call as follows, and you should be fine from here:
onPressed: () {_handleSignIn().whenComplete( ()=>Navigator.of(context).pushNamed("/MyHome"));
}
注意,您不能在自己的外部使用导航器构建方法,因此您始终需要处理小部件中的导航部分。 检查对此答案的评论。
Note that you can not use Navigator outside your build method, hence you always need to handle the navigation parts within your widgets. Check the comment on this answer.
这篇关于行动后生成Flutter路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!