0

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.'),
  );
}

}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

I was able to solve my problem by running my app using flutter run --release i got this solution from https://stackoverflow.com/a/64776426/13866511 however if you have a better solution please leave a comment.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '23 at 15:13