如何处理 Firebase 密码重置电子邮件错误 Flutter

Joe*_*Joe 3 dart firebase firebase-authentication flutter

我正在制作一个简单的密码重置对话框,它将通过 firebase 向用户发送密码重置电子邮件。一切工作正常,但是,我希望能够相应地捕获和处理错误。我似乎不知道如何去做这件事。例如,当用户的互联网连接中断时,我希望他们看到一条消息,表明他们的密码重置电子邮件不是通过小吃栏发送的。

我的代码:

// 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,
      );
    },
  );
}
Run Code Online (Sandbox Code Playgroud)

And*_*rej 6

你可以这样做:

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

在这里阅读更多内容。