Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Firebase password reset email errors Flutter

I am making a simple password reset dialog that will send a password reset email to the user via firebase. Everything works fine, however, I want to be able to catch and handle errors accordingly. I can't seem to figure out how to go about doing this. For example, when the user's internet connection cuts out, I want them to see a message that their password reset email was not sent via a snackbar.

My Code:

// Send user an email for password reset
Future<void> _resetPassword(String email) async {
  await auth.sendPasswordResetEmail(email: email);
}

// This will handle the password reset dialog for login_password
void passwordResetDialog(context, email) {
  displayDialog(
    context,
    title: "Forgot Password?",
    content:
        "We will send you an email with a password reset link. Press on that link and follow the instructions from there.",
    leftOption: "No",
    onPressedLeftOption: () {
      // Close dialog
      Navigator.of(context).pop();
    },
    rightOption: "Yes",
    onPressedRightOption: () {
      
      // Send reset password email
      _resetPassword(email);

      // Close dialog
      Navigator.of(context).pop();

      displaySnackBar(
        context,
        contentText: "Password reset email sent",
        durationTime: 7,
      );
    },
  );
}
like image 998
Joe Avatar asked Oct 30 '25 05:10

Joe


1 Answers

You can do this:

try {
 await auth.sendPasswordResetEmail(email: email);
} on FirebaseAuthException catch (e) {
 print(e.code);
 print(e.message);
// show the snackbar here
}

Read more here.

like image 154
Andrej Avatar answered Nov 01 '25 20:11

Andrej