ERROR:flutter/lib/ui/ui_dart_state.cc(209) instead of response.statusCode == 404
248
Solution 1
the urlString you used is not working.The urlString will be : https://samples.openweathermap.org/ this one is working.
Solution 2
Try to change your url from :
To :
it should return 404 error
Author by
Kosh
Updated on January 03, 2023Comments
-
Kosh over 1 year
Here is my code:
import 'package:http/http.dart' as http; void getData() async { print('getData called'); // Q is not a typo String urlString = 'http://Qsamples.openweathermap.org/'; var url = Uri.parse(urlString); http.Response response = await http.get(url); if (response.statusCode == 200) { print('statusCode = 200'); } else { print('statusCode != 200'); } }
When I make a request to the valid URL everything works like a charm. But when I change it to invalid I get
E/flutter (19828): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: Failed host lookup: 'qsamples.openweathermap.org' (OS Error: No address associated with hostname, errno = 7)
instead of receiving
response.statusCode == 404
.Here there is an example from library's authors where they are advising the same https://pub.dev/packages/http/example
-
Kosh over 2 yearsEdited topic name. I am expecting this URL not to work, but in a controlled way.
-
Hardik Mehta over 2 yearsthats not able to as site not reachable