Reputation: 17
I have this simple function :
Future<Widget> getDog() async{
var res = await http.get(Uri(scheme: "https", host:"random.dog", path:"/woof.json" ));
print(res.statusCode);
if(res.statusCode != 200) throw Exception();
return Image.network(jsonDecode(res.body)["url"]);
}
The http.get
function isn't working anymore. It was fine before, but it's now not returning anything, and the thread is stuck (the print function never gets called). This problem also happens in one of my older project. What could possibly be the cause?
The function is definitely called, as I debugged it and the breakpoint was activated. I tried adding the Internet permission on the android manifest, to no avail. flutter doctor -v
doesn't give me any error. flutter packages get
was also used.
I tried looking at older SO posts, but can't anything like my problem, at least not any with a working fix.
EDIT: Apparently this doesn't work when I'm opening the project at home. But it works when I'm at school for some reason, so I expect a network problem possibly?
Upvotes: -1
Views: 68
Reputation: 4503
Don't you see any exception in console ?
You can also try this:
Future<Widget> getDog() async{
try {
var res = await http.get(Uri(scheme: "https", host:"random.dog", path:"/woof.json" ));
print(res.statusCode);
if(res.statusCode != 200) throw Exception();
return Image.network(jsonDecode(res.body)["url"]);
} catch(error, stacktrace) {
print("${error.toString()}\n${stacktrace.toString()}");
}
}
Like this if an exception is throw you can print it in console.
Upvotes: 0