Im trying to handle firebase auth exceptions, im handle both FirebaseExceptions and PlatformExceptions, however, I can catch FirebaseauthExceptions and general exceptions but, for some reason i cant catch PlatformExceptions. I get the PlatformException "ERROR-WRONG-PASSWORD" when i enter an incorrect password and a correct email combination.
Im using firebase_core: ^2.15.0 and firebase_auth: ^4.7.0
Future<void> login(String email, String password) async {
try {
final credential = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
if (credential != null) {
emit(state.copyWith(user: credential.user));
DatabaseReference userRef = FirebaseDatabase.instance
.ref()
.child('users/${credential.user?.uid}');
userRef.once().then((event) {
final dataSnapShot = event.snapshot;
if (dataSnapShot.value != null) {
emit(state.copyWith(
loadingState: PageLoadingState.submitSuccessEvent));
}
});
}
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
emit(
state.copyWith(
loadingState: PageLoadingState.submitErrorEvent,
errorMessage: 'No user found with the provided credentials.'),
);
} else if (e.code == 'wrong-password') {
emit(
state.copyWith(
loadingState: PageLoadingState.submitErrorEvent,
errorMessage: 'Incorrect email or pasword.'),
);
}
} on PlatformException catch (e) {
if (e.message == 'ERROR_WRONG_PASSWORD') {
emit(
state.copyWith(
loadingState: PageLoadingState.submitErrorEvent,
errorMessage: 'Incorrect email or pasword.'),
);
} else if (e.code == 'ERROR_USER_NOT_FOUND') {
emit(
state.copyWith(
loadingState: PageLoadingState.submitErrorEvent,
errorMessage: 'No User Found.'),
);
}
} catch (e) {
// Handle any other exceptions that might occur during the login process
// For example, network errors or unexpected errorsemit(
emit(
state.copyWith(
loadingState: PageLoadingState.submitErrorEvent,
errorMessage:
'An error occurred during login. Please try again later.'),
);
}
}