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,
);
},
);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With