skip to Main Content

I need to catch and handle errors on my app when an user tries to sing in but I can’t achieve that.
How can I solve it?
Here is my code

  @override
  Future<void> signInUser(UserEntity user) async {
    try {
      if (user.email!.isNotEmpty && user.password!.isNotEmpty) {
        await firebaseAuth.signInWithEmailAndPassword(
            email: user.email!, password: user.password!);
      }
    } on PlatformException catch (e) {
      if (e.code == "ERROR_USER_NOT_FOUND") {
        showToast("user not found");
      } else if (e.code == "INVALID_PASSWORD") {
        showToast("Invalid email or password");
      } else if (e.code == "ERROR_INVALID_EMAIL") {
        showToast("Invalid email");
      }
    }
  }

Here is a image of the error
error

2

Answers


  1. Chosen as BEST ANSWER

    I have solved this problem by disabling breakpoint all exceptions and change tho code to:

      @override
      Future<void> signInUser(UserEntity user) async {
        try {
          if (user.email!.isNotEmpty && user.password!.isNotEmpty) {
            await firebaseAuth.signInWithEmailAndPassword(
              email: user.email!,
              password: user.password!,
            );
          }
        } on FirebaseAuthException catch (e) {
          String errorMessage = "An unknown error occurred.";
    
          switch (e.code) {
            case "user-not-found":
              errorMessage = "User not found.";
              break;
            case "wrong-password":
              errorMessage = "Wrong password.";
              break;
            case "invalid-email":
              errorMessage = "Invalid email.";
              break;
            case "user-disabled":
              errorMessage = "User account is disabled.";
              break;
            case "too-many-request":
              errorMessage = "Too many sign-in attempts. Please try again later.";
              break;
            case "operation-not-allowed":
              errorMessage = "Operation not allowed.";
              break;
            // Add more error codes and their corresponding error messages here.
            default:
              break;
          }
          showToast(errorMessage);
        } catch (e) {
          showToast("An unknown error occurred.");
        }
      }
    

  2. import 'package:flutter/services.dart';
    
    // ...
    
    @override
    Future<void> signInUser(UserEntity user) async {
      try {
        if (user.email!.isNotEmpty && user.password!.isNotEmpty) {
          await firebaseAuth.signInWithEmailAndPassword(
            email: user.email!,
            password: user.password!,
          );
          // Sign-in was successful, you can navigate to the next screen or perform any required actions here.
        }
      } on PlatformException catch (e) {
        String errorMessage = "An unknown error occurred.";
        switch (e.code) {
          case "ERROR_USER_NOT_FOUND":
            errorMessage = "User not found.";
            break;
          case "ERROR_WRONG_PASSWORD":
            errorMessage = "Invalid email or password.";
            break;
          case "ERROR_INVALID_EMAIL":
            errorMessage = "Invalid email.";
            break;
          case "ERROR_USER_DISABLED":
            errorMessage = "User account is disabled.";
            break;
          case "ERROR_TOO_MANY_REQUESTS":
            errorMessage = "Too many sign-in attempts. Please try again later.";
            break;
          case "ERROR_OPERATION_NOT_ALLOWED":
            errorMessage = "Operation not allowed.";
            break;
          // Add more error codes and their corresponding error messages here.
          default:
            break;
        }
        showToast(errorMessage);
      } catch (e) {
        // Handle other types of exceptions that may occur during sign-in.
        showToast("An unknown error occurred.");
      }
    }
    

    If the signInWithEmailAndPassword function is not triggering the
    PlatformException, try using catch (e) instead of on PlatformException
    catch (e) to see if the error can be caught using a general catch
    block.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search