简体   繁体   中英

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

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);
}

`

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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM