如何在flutter/dart中捕获platformException?

Nir*_*net 8 android flutter

我正进入(状态

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

每当我尝试在我的应用程序中弹出谷歌取消登录时。

复制步骤:

在应用程序中,当按下谷歌按钮登录时,谷歌登录页面会出现,其中包含所有可供选择的 Gmail 帐户登录或注册。

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

之后代码中有平台异常说:

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

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

试过 try/catch - 不工作

小智 18

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
}
Run Code Online (Sandbox Code Playgroud)

  • 我的错误是没有调用“await”。如果你没有在 try 中调用 wait ,那么在完成该块后就会引发错误,因此不会被“on”或“catch”块捕获。 (3认同)