如何定义getter,基于错误代码,我可以显示错误消息,而不是拥有多个if` if`语句

发布于 2025-02-02 02:33:22 字数 1065 浏览 2 评论 0原文

我们称API并处理例外。

class Failure with _$Failure  implements Exception  {
  
  factory Failure({
    required FetchError fetchError,
    required ServerError? errorCodeFromServer,
  }) = _Failure;
  

}

并且,基于异常类型,我们在基于捕获错误的情况下丢弃故障时传递了错误代码。现在,该错误代码已传递到集团。而且,在当时的UI屏幕中,我们正在尝试在Bloc侦听器中显示错误消息

        if (state.loginStatus == LoginStatus.error) {
          simpleSnackbarMessage(
            context,
            l10n.[state.error],
            MessageType.failed,
          );
        }

,我们有app_en.arb文件,因为我拥有所有可能的错误消息。但是,当我们试图访问l10n。[state.error],显示出错误。

The getter '[' isn't defined for the type 'AppLocalizations'.

因此,我的问题

如何定义getter,因此,基于错误代码,我可以显示错误消息,而不是拥有语句的多个

谢谢:祈祷:

We are calling API and handling the exception.

class Failure with _$Failure  implements Exception  {
  
  factory Failure({
    required FetchError fetchError,
    required ServerError? errorCodeFromServer,
  }) = _Failure;
  

}

And, based on the exception type We are passing an error code when We are throwing Failure based on catching an error. Now, that error code is passed on to the bloc. And, in then UI screen We are trying to show the error message in bloc listener like this

        if (state.loginStatus == LoginStatus.error) {
          simpleSnackbarMessage(
            context,
            l10n.[state.error],
            MessageType.failed,
          );
        }

now, We have app_en.arb file in that I have all the possible error messages. But, when We are trying to access like l10n.[state.error], it show an error.

The getter '[' isn't defined for the type 'AppLocalizations'.

So, my question

how to define getter so, that based on error code I can show error message instead of having multiple if statement.

enter image description here

Thanks :pray:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文