【发布时间】:2019-11-10 03:54:10
【问题描述】:
在检查 API 端点(确定连接状态)的相对简单的代码块中,我依赖 try..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;
}
}
【问题讨论】:
标签: exception flutter dart visual-studio-code vscode-debugger