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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With