I use simple method to get some data from internet 'http get request' :
`Future<UserModel> getUser(int userId) async {
UserModel user;
try {
final response = await http.get(
"$_baseUrl/users/$userId",
)
.timeout(Duration(seconds: 5))
;
user = userModelFromJson(response.body);
return user;
} on TimeoutException catch (e) {
print('$e in authentication service');
throw e;
} on SocketException catch (e) {
print('$e in authentication service');
throw e;
} catch (e) {
print('$e in authentication service');
throw e;
}
}`
but when i have no internet connection it shows me that error :
`Exception has occurred.
SocketException (SocketException: Failed host lookup:
'jsonplaceholder.typicode.com' (OS Error: No address associated with
hostname, errno = 7))`
whenever i remove the .timeout(Duration(seconds:5)) the code works perfectly , but the socket exception is caught after long time (15-20)seconds to show that there is no internet connection that's why i used timeout, i tried to use multiple packages (http middleware ,http helper ,retry) , i tried to use http.client and close it in finally block and the same error occurred and the app crashes
the image shows the error when the socket exception is thrown and unhandled
it catches the timeout exception as expected but then after another 10-15 seconds it throws an handled socket exception ,why it throws this socket exception and what can i do to avoid this?