代码之家  ›  专栏  ›  技术社区  ›  Charles Jr

如何使用Flatter从Firebase登录打印用户可读的PlatformException部分?

  •  1
  • Charles Jr  · 技术社区  · 7 年前

    在登录页面上工作时,我想显示一个登录错误。我成功地将以下内容打印为 error ...

    平台异常(登录失败、FIRAuthErrorDomain、电子邮件 地址格式不正确。)

    我怎么能只打印/显示

    电子邮件地址格式不正确

    ?

    0 回复  |  直到 7 年前
        1
  •  1
  •   Harsha pulikollu    7 年前

    我并不是说这是最好的实现方式,或者我不知道这是否是唯一的实现方式,但我就是这样实现的。现在它工作得很好。

        void showErrorToast(String error) {
    //I call this method if I got any error message from firebase authentication
            if (error.contains('email address is badly formatted')) {
              error = 'Check your email once..';
            }
            if (error.contains('no user record corresponding to this identifier')) {
              error = 'Hmm... no user found with this email.';
            }
            if (error.contains('password is invalid')) {
              error = 'oho... seems like your password is wrong';
            }
            Fluttertoast.showToast(
                msg: error,
                toastLength: Toast.LENGTH_SHORT,
                gravity: ToastGravity.CENTER,
                timeInSecForIos: 1,
                backgroundColor: Colors.black,
                textColor: Colors.white);
        }
    
        2
  •  1
  •   Yuan25    7 年前

    您可以按如下方式打印所有错误消息:

    例如错误: PlatformException(错误\u电子邮件\u已在使用中,电子邮件地址已被另一个帐户使用,空)

    print(e);//as in above
    print(e.code); //ERROR_EMAIL_ALREADY_IN_USE
    print(e.message);//The email address is already in use by another account.
    print(e.details);//null
    
    推荐文章