Flutter try catch can't catch SocketException ( Exception has occurred. SocketException (SocketException: OS Error: No route to host, errno = 113)
Open
Nobody has claimed this yet.
- Dominant language
- Dart
- Stars
- 1.1k
- Forks
- 419
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 8
Description
Flutter App crashed after getting this exception
Exception has occurred.
SocketException (SocketException: OS Error: No route to host, errno = 113, address = 10.162.18.32, port = 60208)
THE same URL is accessible from the testing phone, Postman.
also tried with
try {
} on SocketException {
}
Still, Exception is not handled.
Future<List<Post>> getPosts() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
print(prefs.get(key));
final value = prefs.get(key) ?? 0;
Map<String, String> headers = {
"Accept": "application/json",
"Authorization": "Bearer $value"
};
print("before res");
var responseJson;
try {
print("ins try get url " + host + "posts");
final response = await http.get(host + "posts",
headers: headers); //Exception on this line
print("final try");
responseJson = _response(response);
} catch (e) {
print(e);
return null;
//throw FetchDataException('No Internet connection');
}
// if (res.statusCode == 200) {
List<dynamic> body = responseJson;
List<Post> posts = body
.map(
(dynamic item) => Post.fromJson(item),
)
.toList();
return posts;
// } else {
// print('A network error occurred');
// //throw "Can't get posts.";
// }
}
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.0.2, on Microsoft Windows [Version 10.0.18363.836], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] VS Code (version 1.54.3)
[√] Connected device (2 available)
• No issues found!
PS C:\Users\nicalp\AndroidStudioProjects\vikasapp> flutter --version
Flutter 2.0.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8962f6dc68 (11 days ago) • 2021-03-11 13:22:20 -0800
Engine • revision 5d8bf811b3
Screenshots
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names getPosts and the await http.get(host + "posts") call but no repository file or test. Start by locating that method, reproduce the SocketException, and trace whether the failure occurs within the awaited call; done means the escaping path and expected handling are confirmed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100