I'm trying to use Google's Firebase Authentication in my flutter application. However, when handling user's login, I cannot seem to find a "code" to differentiate the types of exceptions for me to handle.
Loginhandler.dart:
class LoginHandler {
String signup(_username, _password) async {
bool _valid = false;
final prefs = await SharedPreferences.getInstance();
FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _username, password: _password)
.catchError((e) {
print(e);
print(e.message);
print(e.code);
print(e.details);
});
...
Error outputs:
W/BiChannelGoogleApi(26738): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@4e727e7
W/IInputConnectionWrapper(26738): getCursorCapsMode on inactive InputConnection
I/flutter (26738): PlatformException(exception, The email address is already in use by another account., null)
I/flutter (26738): The email address is already in use by another account.
I/flutter (26738): exception
I/flutter (26738): null
I/flutter (26738): PlatformException(exception, The given password is invalid. [ Password should be at least 6 characters ], null)
I/flutter (26738): The given password is invalid. [ Password should be at least 6 characters ]
I/flutter (26738): exception
I/flutter (26738): null
I have followed this Stack Overflow thread which highlights I can use a switch statement, but the above error output have no error "codes" for me to work with.
I'm able to access the "code" property of the error on Android.
try {
// login logic
} catch (e) {
print('Error: $e');
setState(() {
_isLoading = false;
if (_isIos) {
_errorMessage = e.details;
} else
_errorMessage = e.message;
print(e.code); // can access the code here
});
}
UPDATE:
This was a bug and it's already fixed with this PR https://github.com/flutter/plugins/pull/775.
According to my experience, on iOS there's a bug and you can't get the error codes, only the error text messages. On Android, the error codes do work fine.
On my apps I ended up checking for error text messages.
The issue is already being tracked on flutter, you can subscribe to it and get an update when its fixed.
By the way, this is the pull request that fixes it, however it's not yet merged.
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