flutter google signin stuck loading after select account

Viewed 1809

i am trying to sign in with google first my info i chose google account it stuck in loading without any response or even any console message realted to it here is my code

 GoogleSignInAccount _currentUser;
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
  'email',
  'https://www.googleapis.com/auth/cloud-platform.read-only',
],
);
 @override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
  setState(() {
    _currentUser = account;
  });
});
_googleSignIn.signInSilently();
}

Future<void> _googlelogin() async {
  try {
    final GoogleSignInAccount googleSignInAccount =
        await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;
  } catch (error) {
    print(error);
  }
}

....

 RaisedButton(
      onPressed: () {
       _googlelogin();
      },),
                        ), 

preview of what happening

preview screenshot

preview of messages in console if it related messages

6 Answers

Make sure that you are requesting only information of the scopes you are allowed to access.

In my case, I copied the following example and didn't notice that I don't have access to contacts.

  GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);

Changing to the following worked for me.

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
  ],
);
  1. I deleted the app on firebase console, created new.
  2. I added SHA-1, SHA256 fingerprints in firebase project-level settings.
  3. I also selected my email-id on "Support email" field in project-level settings.
  4. I had com.google.gms:google-services on build.gradle as suggested by @Abdelazeem.

None of them worked for me. Every time I select my Gmail account, it kept loading indefinitely and nothing significant was printed in the console either.

But this worked for me:

pubspec.yaml

  dependencies:
    firebase_core: ^1.7.0
    firebase_auth: ^3.1.2
    googleapis: ^5.0.1
    google_sign_in: ^5.1.1

AuthenticationService.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:googleapis/drive/v3.dart' as drive;
import 'package:google_sign_in/google_sign_in.dart' as signIn;

class AuthenticationService {
  signIn.GoogleSignInAccount? account;

  Future<User?> authenticate() async {
    FirebaseAuth auth = FirebaseAuth.instance;
    User? user;

    final signIn.GoogleSignIn googleSignIn =
        signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.driveFileScope]);

    account = await googleSignIn.signIn();

    if (account != null) {
      final signIn.GoogleSignInAuthentication googleSignInAuthentication =
          await account!.authentication;

      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleSignInAuthentication.accessToken,
        idToken: googleSignInAuthentication.idToken,
      );

      try {
        final UserCredential userCredential =
            await auth.signInWithCredential(credential);

        user = userCredential.user;
      } on FirebaseAuthException catch (e) {
        if (e.code == 'account-exists-with-different-credential') {
          // handle the error here
        } else if (e.code == 'invalid-credential') {
          // handle the error here
        }
      } catch (e) {
        // handle the error here
      }
    }

    return user;
  }
}

i just reset everything and delete firebase project and recreate it from scratch again and magically everything work

  1. Make sure you added those two lines to your app/build.gradle:

apply plugin: 'com.google.gms.google-services' //<== first line

dependencies {
    implementation 'com.google.android.gms:play-services-auth:19.2.0' //<== second line
    ...
    
  1. Also, add to your root/build.gradle inside buildscript.dependencies
classpath 'com.google.gms:google-services:4.3.5'
  1. If you didn't register your app on firebase then go ahead and once you're done, make sure you downloaded the google-services.json file and put it inside your android/app directory.

if Your app is marked as "EXTERNAL" on "OAuth consent screen" - you need to add tester Users

enter image description here

  • If your API project's publishing status is 'Production', some API scopes require verification by Google before they may be presented to the user in a consent screen. Since they are kind of sensitive or restricted scopes.

  • If your API project's publishing status is 'Testing', you need to add test users according to AYMAN's answer.

Related