Home > OS >  Firebase Authentication in Flutter Check if user is a new user
Firebase Authentication in Flutter Check if user is a new user

Time:11-15

I have implemented the following login method and I am trying to use the isNewUser function to push a new screen:

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.

CodePudding user response:

The problem is in the return type, you need change the type from void to dynamic.

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

CodePudding user response:

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.

  1. Add context as parameter in the method googleLogin()

  2. Use this Navigator.push(context,MaterialPageRoute(builder: (context) =>your_new_screen()),); in the condition userCredential.additionalUserInfo!.isNewUser

    In the above replace your_new_screen() with the widget you have returned before ie. SignUpNewUser()

  • Related