在 Flutter 中执行
HTTP request
时,我没有得到任何Exception
,例如 WiFi 打开时的 SocketException
,但实际上没有互联网,而是等待很长时间,直到我获得定义的 TimeoutException
。当一开始有网络连接但突然连接断开时也会出现此问题。
如何做到网络出现问题时直接抛出异常?
这是我当前的代码:
try {
await http
.get(url, headers: headers)
.timeout(const Duration(seconds: 20));
} on SocketException catch (e, st) {
debugPrint('SocketException. Seems to be no internet available');
debugPrint(e.toString());
debugPrint('OS Errorcode: ${e.osError?.errorCode.toString()}');
} catch (e, st) {
debugPrint('Unknown error happen in `tryAndHandle`:');
}