Flutter Exception Error in firebase_auth

Asked

Viewed 177 times

0

Good afternoon, I’m using the firebase_auth plugin: 0.20.0+1 to authenticate user.

When the user matches the login information no error is shown, however when the user is incorrect and an exception is generated, the flutter cannot capture the exception code.

    class UserManager {
  // config FIREBASE
  final FirebaseAuth auth = FirebaseAuth.instance;

  ///
  ////sigin
  Future<void> fazerLogin(
      {Usuario usuario, Function onFail, Function onSuccess}) async {
    // resultado a auteticação
    try {
      final UserCredential result = await auth.signInWithEmailAndPassword(
        email: usuario.email,
        password: usuario.senha,
      );
    } on PlatformException catch (e) {
      onFail(getErrorString(e.code));
    }
  }
}

Figura da Exceção

follows the console information

W/System  ( 8621): Ignoring header X-Firebase-Locale because its value was null.
E/flutter ( 8621): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_auth/wrong-password] The password is invalid or the user does not have a password.
E/flutter ( 8621): #0      MethodChannelFirebaseAuth.signInWithEmailAndPassword
package:firebase_auth_platform_interface/…/method_channel/method_channel_firebase_auth.dart:497
E/flutter ( 8621): <asynchronous suspension>
  • 1

    What’s your question? Want to know how to treat so that Flutter "understands" the exception of "Incorrect password?" and execute the try-catch? It wasn’t very clear...

1 answer

1


The firebase_auth spear a FirebaseAuthException, as explicit in the documentation:

A Firebaseuthexception Maybe thrown with the following error code:

So you need to capture a FirebaseAuthException and not a PlatformException and treat according to error code.

Ex:

Future<void> logInWithEmailAndPassword({
    @required String email,
    @required String password,
}) async {
  try {
    await _firebaseAuth.signInWithEmailAndPassword(
      email: email,
      password: password,
    );
  } on FirebaseAuthException catch (e, s) {
    _handleFirebaseLoginWithCredentialsException(e, s);
  } on Exception catch (e, s) {
    //Outro problema
  }
}

void _handleFirebaseLoginWithCredentialsException(
    FirebaseAuthException e, StackTrace s) {
  if (e.code == 'user-disabled') {
    //'O usuário informado está desabilitado.'
  } else if (e.code == 'user-not-found') {
    //'O usuário informado não está cadastrado.'
  } else if (e.code == 'invalid-email') {
    //'O domínio do e-mail informado é inválido.'
  } else if (e.code == 'wrong-password') {
    //'A senha informada está incorreta.'
  } else {
    //Outro problema
  }
}
  • Hello, with this code the exception is captured, but a crash is generated. So the only option was to disable the crash of vs code. Thank you very much friend !

  • @Pablooliveira if the answer helped you, you can mark it as correct. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.