Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sign In PlatformException(exception, BAD_REQUEST, null, null) on Flutter

This problem occurs on some devices, I tried in Pixel 4 XL API 30, API 29 and real device no problem, but in Pixel 4 API 30 got this error: PlatformException(exception, BAD_REQUEST, null, null)

try {
  final GoogleSignIn googleSignIn = GoogleSignIn();
  final GoogleSignInAccount? googleUser = await googleSignIn.signIn(); // Receive my Google Account details
  if (googleUser == null) return;

  final googleAuth = await googleUser.authentication; // <-- Catch error here

  credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken
  );
} catch (e) {
  print(e); // PlatformException(exception, BAD_REQUEST, null, null)
}

Why does it happen on some devices? Is there a way to solve this problem?

like image 996
Kucing Malaya Avatar asked May 09 '26 11:05

Kucing Malaya


1 Answers

How I resolved it? I was initially creating a single instance of Google Sign In class.

final GoogleSignIn googleSignIn = GoogleSignIn();

So I changed it to make it nullable and then assigned the variable in Sign In Function itself

try {
   _googleSignIn = GoogleSignIn();
   final GoogleSignInAccount? googleSignInAccount = await _googleSignIn?.signIn();
   final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount!.authentication;
   if (googleSignInAuthentication.idToken != null) {
     return googleSignInAuthentication.idToken!;
   } else {
     throw Exception("Google Authentication Failed");
   }    
} catch (e) {
   rethrow;
}

Once it is signed In, after logging out I would call this function

  Future disconnectFirebaseAndGoogle() async {
    await _googleSignIn?.signOut();
    await firebaseAuth.signOut();
    _googleSignIn = null;
  }

This fixed the same problem I had and now it does not give Bad Request error anymore.

like image 66
Shahzad Umar Baig Avatar answered May 10 '26 23:05

Shahzad Umar Baig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!