skip to Main Content

I’m working on a project in Flutter. I use this code:

_googleAuthProvider.setCustomParameters({'prompt': 'select_account'});

    UserCredential userCredential;

    if (kIsWeb) {
      userCredential = await _firebaseAuth.signInWithPopup(_googleAuthProvider);
    } else {
      userCredential =
          await _firebaseAuth.signInWithProvider(_googleAuthProvider);
    }

On web I can choose which account I want to log in with. On mobile (iOS & Android) I don’t get this choice and automatically log in with the previous logged in user.

How can I ensure that I can choose which account I log in with on mobile as well?

I tried several things including changing the customParameters.

2

Answers


  1. Chosen as BEST ANSWER
      final GoogleSignIn googleSignIn = GoogleSignIn();
    
      googleSignIn.disconnect();
    
      final GoogleSignInAccount? googleSignInAccount =
          await googleSignIn.signIn();
    
      if (googleSignInAccount != null) {
        final GoogleSignInAuthentication googleSignInAuthentication =
            await googleSignInAccount.authentication;
    
        final AuthCredential credential = GoogleAuthProvider.credential(
            accessToken: googleSignInAuthentication.accessToken,
            idToken: googleSignInAuthentication.idToken);
    
        userCredential = await _firebaseAuth.signInWithCredential(credential);
      }
    

    This worked for me.


  2. Use this Method

    void _signInWithGoogle() async {
    final GoogleSignInAccount? googleUser =
    await GoogleSignIn(scopes: <String>["email"]).signIn();
    if (googleUser != null) {
      final GoogleSignInAuthentication googleAuth =
      await googleUser.authentication;
      final credential = GoogleAuthProvider.credential(
          accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
      await FirebaseAuth.instance.signInWithCredential(credential);
      String fullName = googleUser.displayName!;
      var names = fullName.split(' ');
      var firstName = names[0];
      var image = googleUser.photoUrl;
      var email = googleUser.email;
    

    }

    When you click on logout use this

    GoogleSignIn googleSignIn = GoogleSignIn(scopes: <String>['email'],);
    googleSignIn.signOut();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search