飞镖飞镖尝试/捕获无法正常工作

发布于 2025-02-10 02:52:50 字数 1083 浏览 3 评论 0 原文

在学习颤音/飞镖时,我遇到了这个问题:

我在我的应用程序中的某个某个地方称呼此方法:

  Future<DatabaseUser> getOrCreateUser({required String email}) async {
    try {
      final user = await getUser(email: email);
      return user;
    } on UserDoesNotExistsException {
      final createdUser = await createUser(email: email);
      return createdUser;
    } catch (e) {
      print(e);
      rethrow;
    }
  }

首次打电话给“ getUser”,“ getuser”会抛出一个userdoesnotexistSexception,这是正确的,所以应该访问行:

} on UserDoesNotExistsException {

这是问题,它总是直接进入行:

} catch (e) {

如果我打印(e),我有userdoesnotexistSexception(请参阅屏幕截图)

让我知道您是否需要更多的上下文,在这种情况下有什么意义?为什么例外不被合适的集团捕获?

任何帮助都将不胜感激

:我关注此37H+视频教程:我在21:36:00我在21:36:00

谢谢

屏幕截图调试器

While learning flutter/dart, I came across this issue:

I'm calling this method in a FutureBuilder somewhere in my app:

  Future<DatabaseUser> getOrCreateUser({required String email}) async {
    try {
      final user = await getUser(email: email);
      return user;
    } on UserDoesNotExistsException {
      final createdUser = await createUser(email: email);
      return createdUser;
    } catch (e) {
      print(e);
      rethrow;
    }
  }

Once called for the first time, the "getUser" throws a UserDoesNotExistsException, which is correct, so it should go to the line:

} on UserDoesNotExistsException {

This is the issue, it always goes straight to the line:

} catch (e) {

If I print (e), I have UserDoesNotExistsException (see screenshot)

Let me know if you need more context here, what could make sense in that situation? Why is the Exception not caught by the right bloc?

Any help would be really appreciated

PS: I'm following this 37h+ video tutorial: https://www.youtube.com/watch?v=VPvVD8t02U8&t=28781s I'm at 21:36:00

Thanks

Screenshot Debugger

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2025-02-17 02:52:51

问题解决了。

正如Julemand101所建议的那样,A print(e.runtimeType) 暴露了 e as 类型 而不是 例外

原因是当例外时缺少 () :默认

if (result.isEmpty) {
  throw UserDoesNotExistsException;
} else {
  return DatabaseUser.fromRow(result.first);
}

情况下而不是

if (result.isEmpty) {
  throw UserDoesNotExistsException();
} else {
  return DatabaseUser.fromRow(result.first);
}

Visual Studio代码对此表示任何警告,但是正如Julemand101所建议的那样,当您不提出错误或例外时,可以将分析器配置为警告:
https://dart-lang.gith.gith.github.github.io/linter/linter/linter/linter/lints/lillly_throw_throw_throw_throw_throw_errow_errors。 html

Issue solved.

As julemand101 suggested, a print(e.runtimeType) exposed the type of e as Type and not Exception.

The reason was a missing () when the Exception is thrown:

if (result.isEmpty) {
  throw UserDoesNotExistsException;
} else {
  return DatabaseUser.fromRow(result.first);
}

instead of

if (result.isEmpty) {
  throw UserDoesNotExistsException();
} else {
  return DatabaseUser.fromRow(result.first);
}

Visual Studio Code doesn't give any warning about this by default, but as julemand101 suggested in the comments, the analyzer can be configured to give a warning when you don't throw an Error or Exception:
https://dart-lang.github.io/linter/lints/only_throw_errors.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文