简体   繁体   English

Flutter 中的 Firebase 身份验证检查用户是否是新用户

[英]Firebase Authentication in Flutter Check if user is a new user

I have implemented the following login method and I am trying to use the isNewUser function to push a new screen:我已经实现了以下登录方法,并且我正在尝试使用isNewUser函数来推送新屏幕:

Future<void> googleLogin() async {
    try {
      final googleUser = await GoogleSignIn().signIn();

      if (googleUser == null) return;

      final googleAuth = await googleUser.authentication;
      final authCredential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );
      UserCredential userCredential =
          await FirebaseAuth.instance.signInWithCredential(authCredential);
      if (userCredential.additionalUserInfo!.isNewUser) {
       return const SignUpNewUser();
      }
    } on FirebaseAuthException catch (e) {
      AlertDialog(
        title: const Text("Error"),
        content: Text('Failed to sign in with Google: ${e.message}'),
      );
    }
  }

I get the following error:我收到以下错误:

A value of type 'SignUpNewUser' can't be returned from the method 'googleLogin' because it has a return type of 'Future<void>'.

I'm pretty sure that I placed it in the correct spot to implement the function, but I have no idea how to do it in a Future.我很确定我将它放在正确的位置来实现该功能,但我不知道如何在 Future 中做到这一点。

问题在于返回类型,您需要将类型从 void 更改为 dynamic。

Future<dynamic> googleLogin() async {...}

you can return a widget directly but that doesn't makes sense so you need to use Navigator in order to push to a new screen.您可以直接返回一个小部件,但这没有意义,因此您需要使用Navigator才能推送到新屏幕。

  1. Add context as parameter in the method googleLogin()googleLogin()方法中添加context作为参数

  2. Use this Navigator.push(context,MaterialPageRoute(builder: (context) =>your_new_screen()),);使用这个Navigator.push(context,MaterialPageRoute(builder: (context) =>your_new_screen()),); in the condition userCredential.additionalUserInfo!.isNewUser在条件userCredential.additionalUserInfo!.isNewUser

    In the above replace your_new_screen() with the widget you have returned before ie.在上面用你之前返回的小部件替换your_new_screen() SignUpNewUser()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM