如何使用命名路由从googleSignIn将来的方法导航



我在一个名为auth.dart的单独文件中的类AuthService中有googleSignIn方法。

// in AuthService class
/.../
Future<FirebaseUser> googleSignIn() async {
    loading.add(true);
    GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    final FirebaseUser user = await _auth.signInWithCredential(credential);
    updateUserData(user);
    print("signed in " + user.displayName);
    loading.add(false);
    return user;
  }
/.../

在登录页面(LogInPage.dart(中,我有一个谷歌登录按钮。

// in _LogInPageState class
/.../
socialIcons(
  colors: [Color(0xffcb2d3e), Color(0xffef473a)],
  icon: Icon(
    FontAwesomeIcons.google,
    color: Colors.white,
  ),
  onPressed: () {
    final login = authService.googleSignIn();
    login.then(/*navigate to home page using named routes*/);
  },
),
/.../

我正在尝试在 onPress 中完成 googleSignIn(( 后执行以下代码。

Navigator.of(context).pushNamedAndRemoveUntil(
  '/home', (Route<dynamic> route) => false);

我该怎么做?

当你的googleSignIn回归未来时。您可以简单地使用then并导航到所需的小部件。

    googleSignIn().then((FirebaseUser user) {
      //Navigation code here
    }).catchError((e) {
      //Error if any
    });

相关内容

最新更新