When doing a HTTP request
in Flutter I do not get any Exception
like SocketException
when WiFi is on but there is actually no internet, rather it waits long time until I get the defined TimeoutException
. Also this issue appears when there is network connection in the beginning but suddenly the connection drops.
How do I make it that it directly gives an exception whenever network issues occur?
This my current code:
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`:');
}