I'm working on authentication part of my flutter project. I want to carryout out exception handling for login when the phone is not connected to internet. The app has 3 auth providers: Phone, GoogleSignIn, and email. The error message is displayed properly for phone signin and email sigin using FirebaseException, which displays the error message. But for GoogleSignIn, if the device is not connected to the internet the code fails at :
final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
Since the execution fails at this point I think FirebaseAuthException doesnt work as it is not supported in googleSignIn.
For which FirebaseAuthException doesnt work, but only "Exception" works. And i dont get the proper error message using Exception in an alert Dialog.
Code for Email SignIn with exception handling.
Future<void> signInWithEmailAndPassword(
String email,
String password,
void Function(FirebaseAuthException e) errorCallback,
) async {
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
} on FirebaseAuthException catch (e) {
errorCallback(e);
}
}
Error Dialog which gets called in errorCallback(e)
void _showErrorDialog(BuildContext context, String title, Exception e) {
showDialog<void>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(
title,
style: const TextStyle(fontSize: 24),
),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(
'${(e as dynamic).message}',
style: const TextStyle(fontSize: 18),
),
],
),
),
actions: <Widget>[
ElevatedButton(onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK',
style: TextStyle(color: Colors.deepPurple),
),)
],
);
},
);
}
Exception handling using FirebaseAuthException for email
Exception handling using FirebaseAuthException for email
Google SignIn
Future<void> signInWithGoogle (
void Function(Exception e) errorCallback,
) async {
try{
final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
final GoogleSignInAuthentication? googleAuth = await googleUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth!.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
}
on Exception catch (e) {
print(e);
errorCallback(e);
}
}
Exception handling for google login using Exception instead of FirebaseAuthException
Error printed in console I/flutter ( 4383): PlatformException(network_error,com.google.android.gms.common.api.ApiException: 7: , null, null)
P.S I just want the proper network error to be displayed in the alert dialog and not " com.google.android.gms.common.api.ApiException: 7"