How to stop dio get when the internet is off?

Viewed 34

the request is just waiting and not resulting an error, i want an error to be produced when there is no internet but it's just waiting forever

i tried

sendTimeout: 600000,

receiveTimeout: 600000,

but same result

vscode screenshot

1 Answers

You need to wrap your logic inside try/catch block.

try {
  var response = await _dio.getUri(
    Uri.parse(uri));
  if (response.statusCode == 200) {
    return response.data;
  } else {
    throw response;
  }
} on SocketException catch (e) {
  throw SocketException(e.toString());
} on FormatException catch (_) {
  throw FormatException("Unable to process the data");
} catch (e) {
  throw e;
}
Related