sendPasswordResetEmail 中的 Flutter FirebaseAuthException

问题描述 投票:0回答:1

如果用户想要恢复密码,请激活以下功能

void resetPassword() async {
      final isValid = formKey.currentState!.validate();

      if (!isValid) {
        return;
      }

      formKey.currentState!.save();

      try {
        final auth = FirebaseAuth.instance;
        await auth.sendPasswordResetEmail(email: email);
        changeIsResetValue(true, "$unicodeCheck Password reset link has been sent to your email");
      
      } on FirebaseAuthException catch (e) {
        print(e.code);
        if (e.code == 'user-not-found') {
          changeIsResetValue(true, "$xUnicode This user does not exist");

        } else {
          
          if (!context.mounted) {
            return;
          }
          AlertDialogs().fatalErrorDialogMessage(context, e.toString());
        }
      }

      // close keyboard
      if (!context.mounted) {
        return;
      }
      FocusScope.of(context).unfocus();
    }

现在,如果我输入现有的电子邮件,它可以正常工作,并且我可以更改密码,问题是它似乎永远无法进入

on FirebaseAuthException catch
,即使我输入未注册的电子邮件,我也永远不会收到错误,就好像它永远不会一样进入 catch 块。

如果用户不存在,如何返回错误?我不明白什么?

flutter firebase firebase-authentication
1个回答
0
投票

继续使用此代码来更好地理解该方法返回或抛出的内容:

void resetPassword() async {
  final isValid = formKey.currentState!.validate();

  if (!isValid) {
    return;
  }

  formKey.currentState!.save();

  try {
    final auth = FirebaseAuth.instance;
    await auth.sendPasswordResetEmail(email: email);
    changeIsResetValue(true, "$unicodeCheck Password reset link has been sent to your email");
  
  } on FirebaseAuthException catch (e) {
    print("FirebaseAuthException code: ${e.code}");
    if (e.code == 'user-not-found') {
      changeIsResetValue(true, "$xUnicode This user does not exist");
    } else {
      print("Unhandled FirebaseAuthException: ${e.toString()}");
      if (!context.mounted) {
        return;
      }
      AlertDialogs().fatalErrorDialogMessage(context, e.toString());
    }
  } catch (e, stackTrace) {
    print("Exception occurred: $e\nStack Trace: $stackTrace");
    if (!context.mounted) {
      return;
    }
    AlertDialogs().fatalErrorDialogMessage(context, e.toString());
  }

  // close keyboard
  if (!context.mounted) {
    return;
  }
  FocusScope.of(context).unfocus();
}
 
© www.soinside.com 2019 - 2024. All rights reserved.