The*_*kZn 15 exception dart visual-studio-code flutter vscode-debugger
在检查 API 端点(确定连接状态)的相对简单的代码块中,我依靠 atry..catch作为机制来验证应用程序是否可以与服务器通信。
我遇到的问题是,在调试时,调试器总是在连接线上停止(当应用程序离线时),即使我在内部处理错误。
Future<bool> isOnline() async {
try {
// VSCode debugger always stops on this line when no connection
await http
.get('${consts.apiBaseUrl}/api/ping')
.timeout(Duration(seconds: normalTimeoutLength))
.catchError(
(_) {
// Trying catchError on the Future
_isOnline = false;
return false;
},
);
_isOnline = true;
return true;
} on HttpException catch (_) {
// Trying to catch HTTP Exceptions
_isOnline = false;
return false;
} on SocketException catch (_) {
// Trying to catch Socket Exceptions
_isOnline = false;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*eny 11
这是 Dart VM 的一个限制。它不能正确检测捕获的异常,catchError()因此它会导致调试器暂停它们。这里有一些关于这个的讨论:
https://github.com/flutter/flutter/issues/33427#issuecomment-504529413
如果您单击继续/恢复,行为应该没有区别,但作为一种解决方法,您可以将代码转换为使用真实的try/catch而不是catchError()或取消选中调试侧栏中的选项以中断未捕获的异常(尽管显然这会影响真实未捕获的异常也是如此 - 尽管在 Flutter 中它们不太常见,因为框架捕获了大多数异常)。
| 归档时间: |
|
| 查看次数: |
5288 次 |
| 最近记录: |