行动后生成Flutter路线?
问题内容:
成功登录后,我试图自动路由回到菜单屏幕,但无法生成Widget构建和/或上下文。我正在使用无状态小组件进行此操作。这是我的代码。我想在最后一个打印语句之后调用路由…
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);
}
问题答案:
根据您对问题的了解,基本上,您需要设置登录按钮,以将用户重定向到成功登录后希望到达的任何位置。
您可以按以下方式设置onPressed调用,并且从此处可以正常进行:
onPressed: () {_handleSignIn().whenComplete( ()=>Navigator.of(context).pushNamed("/MyHome"));
}
请注意 ,您不能在构建方法之外使用导航器,因此,您始终需要处理小部件中的导航部分。 检查对此答案的评论。