Search code examples
flutterdartvisual-studio-codemobiletry-catch

How can I print a message using try and catch in Flutter+Firebase on VSCode?


Following code is implementation of _LoginPageState:

  final emailController = TextEditingController();
  final passwordController = TextEditingController();

  void signIn() async{
    // loading circle
    print("SignIn");
    showDialog(
      context: context,
      builder: (context) {
        return const Center(
          child: CircularProgressIndicator(),
        );
      },
    );
    // dealing with incorrect email or password
    try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(
        email: emailController.text,
        password: passwordController.text,
    );
    // topmost component stacked will be popped (opposite of push)
    Navigator.pop(context);
    } on FirebaseAuthException catch (e) {
      Navigator.pop(context);
      if (e.code == 'user-not-found'){
        print('wrong user');
        wrongEmailMessage();
      } else if (e.code == 'wrong-password'){
        print('wrong pass');
        wrongPassMessage();
      }
    }
  }

  void wrongEmailMessage() {
    showDialog(
        context: context,
        builder: (context) {
          return const AlertDialog(
            backgroundColor: Colors.deepPurple,
            title: Center(
              child: Text('Incorrect Email', style: TextStyle(color: Colors.white),),
              ),
          );
        });
  }

I was just copying down a YouTube video to practice Dart/Flutter, but I did not get any error messages set inside the try and catch like "wrong user" and "wrong pass" on the console when I type a wrong email or password. What it shows is just a loading circle, but not anything other on UI and console. The console shows the output "I/flutter ( 8678): SignIn," and I copied and pasted the exact same code that the video shows: https://github.com/mitchkoko/EmailLoginLogout/blob/main/lib/pages/login_page.dart

Therefore, the problem should be somewhere in try or my environment, but I could not figure out. If I type the right email and password, the program works correctly and navigates to home page. I am using Pixel_3a_API...7_x86 android x64 emulator and Debug Console on VSCode. I tried both run without debugging and flutter run on terminal, but it did not make any change.

Any help will be appreciated,


Solution

  • Your code should be fine. The issue is probably due to catching invalid error codes from e.code.

    In your code:

    on FirebaseAuthException catch (e) {
          Navigator.pop(context);
          if (e.code == 'user-not-found'){
            print('wrong user');
            wrongEmailMessage();
          } else if (e.code == 'wrong-password'){
            print('wrong pass');
            wrongPassMessage();
          }
    

    You are specifically looking for the error codes of user-not-found and wrong-password. However, it's possible that your getting a different error message other then what your specifically catching in e.code.

    For example, when I put in a wrong password, I get the error message of:

    invalid-credential
    

    And not

    wrong-password
    

    So, to debug the problem, just use a print statement and view the error code:

    on FirebaseAuthException catch (e) {
          print('catching, the error code is "${e.code}"'); // --> add a print here
          Navigator.pop(context);
          if (e.code == 'user-not-found') {
            print('wrong user');
    

    You can also see a list of Firebase's Admin API errors (which doesn't include wrong-password as an error message).