为最终用户处理 Flutter 上的 Firebase 身份验证错误

Posted

技术标签:

【中文标题】为最终用户处理 Flutter 上的 Firebase 身份验证错误【英文标题】:Handle Firebase Auth Errores on Flutter for End Users 【发布时间】:2021-08-18 07:58:27 【问题描述】:

每次我得到一个错误,例如在登录屏幕上:“无效密码”,我都会得到一个带有如下消息的字符串:

如何将此消息更改为用户友好的消息?我尝试使用在另一篇文章中找到的以下代码:

class Errors 
  static String show(String errMsg) 
    switch (errMsg) 
      case 'ERROR_EMAIL_ALREADY_IN_USE':
        return "This e-mail address is already in use, please use a different e-mail address.";

      case 'ERROR_INVALID_EMAIL':
        return "The email address is badly formatted.";

      case 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL':
        return "The e-mail address in your Facebook account has been registered in the system before. Please login by trying other methods with this e-mail address.";

      case 'ERROR_WRONG_PASSWORD':
        return "E-mail address or password is incorrect.";

      default:
        return "An error has occurred";
    
  


但它总是在屏幕上显示默认错误。我尝试在这些案例上使用“firebase_auth/wrong-password”或“wrong-password”,但没有奏效。有没有人有更好的解决方案?我是新手!

【问题讨论】:

***.com/questions/56113778/… 我想这正是你要找的东西 我去看看试试看! 【参考方案1】:

尝试在此处阅读此文档:

https://firebase.flutter.dev/docs/auth/usage

正如您在文档中看到的那样,字母是小写字母,并由“-”分隔。

【讨论】:

【参考方案2】:
I searched a bit on the post from ZOthix and i did find the solution.

I post it here for anyone who needs it:
class Errors 
  static String show(String errMsg) 
    switch (errMsg) 
      case "email-already-in-use":
        return "Email already used. Go to login page.";
        break;
      case "wrong-password":
        return "Wrong email/password combination.";
        break;
      case "user-not-found":
        return "No user found with this email.";
        break;
      case "user-disabled":
        return "User disabled.";
        break;
      case "ERROR_TOO_MANY_REQUESTS":
      case "operation-not-allowed":
        return "Too many requests to log into this account.";
        break;
      case "ERROR_OPERATION_NOT_ALLOWED":
      case "operation-not-allowed":
        return "Server error, please try again later.";
        break;
      case "invalid-email":
        return "Email address is invalid.";
        break;
      default:
        return "Login failed. Please try again.";
        break;
    
  


【讨论】:

以上是关于为最终用户处理 Flutter 上的 Firebase 身份验证错误的主要内容,如果未能解决你的问题,请参考以下文章

身份验证在 FirebaseStorage Flutter 中不起作用

构建 Flutter iOS 应用程序时出错 - Firebase

Firebase for Flutter 登录构建失败

未处理的异常:MissingPluginException(对于 Flutter 上的许多包,在通道 plugins.flutter.io/ 上找不到方法 getAll 的实现

Flutter 现有项目到 WebApp + Firebase (Firestore + Hosting)

Flutter 无法为具有非最终字段的类定义 const 构造函数