Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sign In current user return null Flutter

I want to integrate my app with Calendar API from Google. And in order to use it, I have to have an AuthClient (which is obtained from _googleSignIn.authenticatedClient();). The problem is, my GoogleSignIn().currentUser always return null and I don't know why. I already use Firebase Auth and Google Sign In.

This is my signInWithGoogle method:

  Future signInWithGoogle() async {
    try {
      await GoogleSignIn().disconnect();
      await FirebaseAuth.instance.signOut();
    } catch (e) {
      print(e.toString());
    }

    // Trigger the authentication flow
    final GoogleSignInAccount? googleUser = await GoogleSignIn(scopes: [CalendarApi.calendarScope]).signIn();

    // Obtain the auth details from the request
    final GoogleSignInAuthentication googleAuth =
        await googleUser!.authentication;

    // Create a new credential
    final credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    // Once signed in, return the UserCredential
    UserCredential result =
        await FirebaseAuth.instance.signInWithCredential(credential);
    User user = result.user!;

    // note: this line always return null and I don't know why
    print('current user auth ${GoogleSignIn().currentUser.toString()}');
    return _userFromFirebaseUser(user);
  }

Did I do something wrong in my code? Any help will be appreciated, thank you!

like image 817
William Edward Avatar asked Sep 11 '25 21:09

William Edward


1 Answers

I also had the issue of GoogleSignIn().currentUser always being null but managed to (finally!) fix it by only initialising GoogleSignIn() once.

For those who want more details: I did this by creating a class called AuthManager that handles everything authentication-related, and making GoogleSignIn one of the parameters required to initialise it (since I'm using Firebase, this was the other parameter):

class AuthManager {
  final FirebaseAuth _auth;
  final GoogleSignIn _googleSignIn;
  AuthManager(this._auth, this._googleSignIn);
  
  Future signInWithGoogle() async {
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
    // etc....
  }

  GoogleSignInAccount? get googleAccount {
    return _googleSignIn.currentUser;
  }
}

And I initiaised by AuthManager class ONCE at the top of my app in a Provider, meaning that I can access it anywhere in my app.

In main.dart:

@override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        // To use AuthManager throughout app without initialising it each time
        Provider<AuthManager>(
          create: (_) => AuthManager(
            FirebaseAuth.instance,
            GoogleSignIn(scopes:  
                // Put whatever scopes you need here
            ), 
          ),
        ),
// etc...

(Note: I used MultiProvider as I had other things I wanted to put, but if you only have one, you can obviously just go straight to Provider).

Now I can successfully get the current google user by getting googleAccount through my AuthManager class.

like image 86
Sparkringo Avatar answered Sep 14 '25 11:09

Sparkringo