解决 PlatformException (PlatformException(user-not-found)) Firebase

时间:2021-02-17 19:24:11

标签: firebase flutter firebase-authentication

我不知道如何处理 Firebase 启动的这个异常。

Exception has occurred.
PlatformException (PlatformException(user-not-found, There is no user record corresponding `to this identifier. The user may have been deleted., 
{code: user-not-found, message: There is no user record corresponding to this identifier. The user may have been deleted., 
nativeErrorMessage: There is no user record corresponding to this identifier. The user may have been deleted., nativeErrorCode: 17011, additionalData: {}}, null)).

这是代码,它来自“loginWithEmailAndPassword”:

Future<void> submit(
      context, FirebaseAuth authF, String email, String password) async {
    try {
      final userCredential = await authF.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      return;
    } on FirebaseException catch (e) {
      if (Platform.isIOS) {
        LogInCupertinoDialogue(context);
      } else {
        LogInAndroidDialogue(context);
      }
      return;
    }
  }

用户不存在,这是我需要处理的。 编辑:对于“处理”,我的意思是如果非注册用户尝试登录我的应用程序崩溃。我不要!如果它崩溃,只需启动窗口对话框。

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,以下内容应该可以解决问题:

      } on FirebaseException catch (e) {
        if (e.code == 'user-not-found') {

           // Display the error message that user doesn't exists

        } else {

           // Display a more global error message

        }
      } ...          

您可以从异常详细信息中检测代码:

<块引用>

PlatformException (PlatformException(user-not-found, There is no user 与此标识符对应的记录。用户可能已经 删除。,{代码:用户未找到,...


注意:您会在 doc 中找到 signInWithEmailAndPassword() 返回的潜在错误代码列表。

答案 1 :(得分:0)

您可以使用异常代码来检测不同的异常,然后使用对话框将其显示给用户,这是一个简单的示例:-

Future<User> signin(String email, String password, BuildContext context) async {
  await Firebase.initializeApp();
  try {
    UserCredential result =
        await auth.signInWithEmailAndPassword(email: email, password: email);
    User user = result.user;
    return Future.value(user);
  } catch (e) {
    print(e.code);
    switch (e.code) {
      case 'ERROR_INVALID_EMAIL':
        showErrDialog(context, e.code);
        break;
      case 'ERROR_WRONG_PASSWORD':
        showErrDialog(context, e.code);
        break;
      case 'ERROR_USER_NOT_FOUND':
        showErrDialog(context, e.code);
        break;
      case 'ERROR_USER_DISABLED':
        showErrDialog(context, e.code);
        break;
      case 'ERROR_TOO_MANY_REQUESTS':
        showErrDialog(context, e.code);
        break;
      case 'ERROR_OPERATION_NOT_ALLOWED':
        showErrDialog(context, e.code);
        break;
    }

showErrDialog(BuildContext context, String err) {
  FocusScope.of(context).requestFocus(new FocusNode());
  return showDialog(
    context: context,
    child: AlertDialog(
      title: Text("Error"),
      content: Text(err),
      actions: <Widget>[
        OutlineButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text("Ok"),
        ),
      ],
    ),
  );
}

}