Home > database >  I'm trying to signOut with google in Flutter with Firebase and it doesn't work
I'm trying to signOut with google in Flutter with Firebase and it doesn't work

Time:12-16

I'm using Flutter with Firebase. When I try to logout with email and password, it works good but when I try with google it doesn't work.

Here is my code: `

try {
    switch (user.providerData[0].providerId) {
    case 'password':
      await FirebaseAuth.instance.signOut();
      break;
    case 'google.com':
      final GoogleSignIn googleSignIn = GoogleSignIn();
      await googleSignIn.signOut();
      break;
}
} on FirebaseAuthException catch (e) {
    showAuthException(e, context);
}

`

I'm trying this

`

try {
    switch (user.providerData[0].providerId) {
    case 'password':
      await FirebaseAuth.instance.signOut();
      break;
    case 'google.com':
      final GoogleSignIn googleSignIn = GoogleSignIn();
      await googleSignIn.signOut();
      break;
}
} on FirebaseAuthException catch (e) {
    showAuthException(e, context);
}

`

CodePudding user response:

Please refer to this code :) and this will do the trick.


your Code:

final GoogleSignIn googleSignIn = GoogleSignIn();
await googleSignIn.signOut();

the problem is you have created googleSignIn variable and signOut from that variable.

you can also try to check using googleSignIn.isSignedIn(); this will return bool value.


Working Code.

class GoogleServiceProvider extends ChangeNotifier {
  static final GoogleSignIn _googleSignIn = GoogleSignIn(); // <----

  GoogleSignInAccount? _user;

  GoogleSignInAccount? get user => _user;

  Future<GoogleSignInAccount?> logInWithGmail() async {
    final googleUser = await _googleSignIn.signIn();
    if (googleUser != null) {
      _user = googleUser;
      notifyListeners();
    }
    return null;
  }

  Future logOut() async {
    await _googleSignIn.signOut();
    notifyListeners();
  }
}
  • Related