【问题标题】:How to catch platformException in flutter/dart?如何在颤振/飞镖中捕获平台异常?
【发布时间】:2019-09-28 13:16:20
【问题描述】:

我来了

发生了异常。 PlatformException (PlatformException(sign_in_canceled, com.google.android.gms.common.api.ApiException: 12501: , null))

每当我尝试取消登录时,我的应用中都会弹出 google。

复制步骤:

在应用程序中,当按下使用 google 登录按钮时,会出现 google 登录页面,其中包含所有 gmail 帐户供您选择登录或注册。

触摸弹出窗口的外部区域以取消登录,然后关闭弹出窗口。

之后代码中出现平台异常:

发生了异常。 PlatformException (PlatformException(sign_in_canceled, com.google.android.gms.common.api.ApiException: 12501: , null))

现在登录按钮不起作用,应用程序崩溃,

试过 try/catch - 不工作

【问题讨论】:

  • 请添加显示您在做什么的代码。你在 api 调用中使用 await 吗?
  • 我也有同样的问题

标签: android flutter


【解决方案1】:
try {
  final res = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  if (res != null) loggedUser = res.user;
} on PlatformException catch (err) {
  // Handle err
} catch (err) {
  // other types of Exceptions
}

【讨论】:

  • 包含一些关于代码如何回答问题或解决问题的解释通常很有用。
  • 我的错误是没有打电话给await。如果你没有在 try 中调用 await ,那么在你完成块后错误会出现,因此不会被 oncatch 块捕获。
【解决方案2】:

我最近也遇到了这个错误,我发现.catchError() 回调没有在调试模式下被调用(当你点击 VSCode 中的Run->Start Debugging 按钮时)。

但是,当您输入 flutter run -d 时,.catchError() 方法将被回调,因为它未处于调试模式。

要获取您喜欢的模拟器的代码,请将这行代码粘贴到终端中:

instruments -s devices

如果这不起作用,您也可以尝试粘贴:

xcrun simctl list

.catchError() 方法将与以前不同地被调用,其中的代码将按预期执行!

此外,应用程序不会再因PlatformException() 而崩溃,而是您会得到这样的日志:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null

我在 Google 登录中也遇到过这个问题,.catchError() 没有被调用!

总之,如果你在处理 Firebase Authentication 的错误时遇到了一些错误,你应该首先尝试通过终端运行。谢谢,我希望这对您有所帮助并相信我,它应该对您有用!

【讨论】:

    【解决方案3】:

    显然,此问题仍未解决,如果您从终端使用 flutter run -d 运行颤振应用程序,则不会发生此问题 不要担心应用程序的发布版本。它不会在发布版本中崩溃或冻结。

    【讨论】:

      【解决方案4】:

      这是捕获平台异常的方法

      try {
        //
      } on PlatformException catch (e) {
        if (e.code == '???') {
           // 
        }
      } catch (e) {
        //
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-28
        • 1970-01-01
        • 2019-12-29
        • 2019-05-07
        • 2020-08-12
        • 2020-11-05
        • 2018-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多