在学习颤音/飞镖时,我遇到了这个问题:
我在我的应用程序中的某个某个地方称呼此方法:
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
发布评论
评论(1)
问题解决了。
正如Julemand101所建议的那样,A print(e.runtimeType) 暴露了 e as 类型 而不是 例外 。
原因是当例外时缺少 () :默认
情况下而不是
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:
instead of
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