dart Flutter不是Firebase错误,即使我设置了catch错误

bbuxkriu  于 2023-10-13  发布在  Flutter
关注(0)|答案(1)|浏览(105)

我在flutter中创建了一个登录和登录页面,当我尝试登录时,它可以捕获错误,但当我注册或创建一个具有相同错误处理的用户时,它无法捕获错误并冻结应用程序并将我带到一个firebase异常文件。这里有一个视频来展示我所说的:
https://youtu.be/Tk5UgwSe9z4
下面是函数:

signUserUp()

void signUserUp(String email, String password, BuildContext context) {
  FirebaseAuth.instance
      .createUserWithEmailAndPassword(email: email, password: password)
      .then((value) => Navigator.pushReplacement(
          context, CustomPageRoute(child: const Home())))
      .catchError((error) {
    String errorMessage;
    if (error is FirebaseAuthException) {
      switch (error.code) {
        case 'network-error':
          errorMessage = 'Network error occured';
          break;
        case 'invalid-email':
          errorMessage = 'The email address is not valid';
          break;
        case 'user-disabled':
          errorMessage = 'The user account has been disabled';
          break;
        case 'user-not-found':
          errorMessage = 'No User Found';
          break;
        case 'wrong-password':
          errorMessage = 'The password is not correct';
          break;
        default:
          errorMessage = error.message ?? 'An unknown error occurred';
      }
    } else {
      errorMessage = error.toString();
    }
  });
}
// Then I use the "showDialog()" function to show a dialog box with the "errorMessage"

我尽量保持问题的简洁,如果我需要提供任何额外的信息,请让我知道

mf98qq94

mf98qq941#

Future<void> signUserUp(String email, String password, BuildContext context) async {
  try {
    await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password);
    Navigator.pushReplacement(context, CustomPageRoute(child: const Home()));
  } catch (error) {
    String errorMessage;
    if (error is FirebaseAuthException) {
      switch (error.code) {
        case 'email-already-in-use':
          errorMessage = 'The email address is already in use by another account';
          break;
        case 'weak-password':
          errorMessage = 'The password is too weak';
          break;
        // ... Add other error cases related to sign-up as necessary
        default:
          errorMessage = error.message ?? 'An unknown error occurred';
      }
    } else {
      errorMessage = error.toString();
    }
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Error'),
          content: Text(errorMessage),
          actions: <Widget>[
            FlatButton(
              child: Text('Close'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

需要考虑的几点:
我把函数改成了pwc。try块中抛出的错误将被catch块捕获。我添加了一些与注册相关的额外错误代码(例如,email-already-in-use和weak-password),但您可能希望根据Firebase文档添加更多错误代码。showDialog()函数在catch块内调用,确保在发生错误时调用它。如果在这些更改之后,您仍然面临问题,您可能需要进一步调试:
检查代码的其他部分是否有错误,特别是在调用signUserUp()之前/之后。确保您的Firebase项目已正确设置身份验证,特别是使用电子邮件/密码方法。确保pubspec.yaml中的依赖项是最新的并且兼容。

相关问题