如何处理扑扑的Firebase Auth异常 [英] How to Handle Firebase Auth exceptions on flutter

查看:175
本文介绍了如何处理扑扑的Firebase Auth异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问有人知道如何在Flutter上捕获Firebase Auth异常并显示它们吗?

Please does anyone know how to catch firebase Auth exceptions on flutter and display them?

注意:我对控制台不感兴趣(catcherror((e)print(e))

Note: I am not interested in the console (catcherror((e) print(e))

我需要更有效的方法,例如用户不存在",这样我才能将其传递给字符串并显示出来.

I need something that is more effective, e.g " user doesn't exist" So that I can then pass it to a string and display it.

处理这个问题几个月了.

Been dealing with this for months.

谢谢.

我尝试用//errorMessage = e.toString();替换print(e);然后将其传递给函数,所有的努力都是徒劳的.

I have tried replacing print(e) with // errorMessage=e.toString(); and then passing it to a function, all efforts have been futile.

    FirebaseAuth.instance
              .signInWithEmailAndPassword(email: emailController.text, password: passwordController.text)
              .then((FirebaseUser user) {
                _isInAsyncCall=false;
            Navigator.of(context).pushReplacementNamed("/TheNextPage");

          }).catchError((e) {
           // errorMessage=e.toString();
            print(e);
            _showDialog(errorMessage);

            //exceptionNotice();
            //print(e);

我希望能够提取异常消息并将异常消息传递到一个对话框,然后可以向用户显示该对话框.

I want to be able to extract the exception message and pass the exception message to a dialog that I can then display to the user.

推荐答案

(21/02/20)该答案很旧,其他答案包含跨平台解决方案,因此您应该首先看看他们的解决方案这是一种备用解决方案.

firebase auth插件实际上还没有合适的跨平台错误代码系统,因此您必须独立处理android和ios的错误.

The firebase auth plugin doesn't really have a proper cross-platform error code system yet so you have to handle errors for android and ios independently.

我当前正在使用此github问题上的临时修复程序:#20223

I'm currently using the temporary fix from this github issue: #20223

请注意,因为它已修复 temp ,所以不要指望它作为永久解决方案是完全可靠的.

Do note since its a temp fix, don't expect it to be fully reliable as a permanent solution.

enum authProblems { UserNotFound, PasswordNotValid, NetworkError }

try {
  FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: email,
      password: password,
  );
} catch (e) {
  authProblems errorType;
  if (Platform.isAndroid) {
    switch (e.message) {
      case 'There is no user record corresponding to this identifier. The user may have been deleted.':
        errorType = authProblems.UserNotFound;
        break;
      case 'The password is invalid or the user does not have a password.':
        errorType = authProblems.PasswordNotValid;
        break;
      case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
        errorType = authProblems.NetworkError;
        break;
      // ...
      default:
        print('Case ${e.message} is not yet implemented');
    }
  } else if (Platform.isIOS) {
    switch (e.code) {
      case 'Error 17011':
        errorType = authProblems.UserNotFound;
        break;
      case 'Error 17009':
        errorType = authProblems.PasswordNotValid;
        break;
      case 'Error 17020':
        errorType = authProblems.NetworkError;
        break;
      // ...
      default:
        print('Case ${e.message} is not yet implemented');
    }
  }
  print('The error is $errorType');
}

这篇关于如何处理扑扑的Firebase Auth异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆