How to process onError
in the following request? On print
statement there is an error: The return type 'void' isn't a 'FutureOr<List<dynamic>>', as required by the closure's context.
Future<List<dynamic>> _getJson(String url) => HttpClient()
.getUrl(Uri.parse(url))
.then((req) => req.close())
.then((response) => response.transform(utf8.decoder).join())
.then((jsonString) => json.decode(jsonString) as List<dynamic>).onError((error, stackTrace) => print(error.toString()));
>Solution :
Your are experiencing this kind of error, because the method’s return type is Future<List>, but inside the onError method you are trying to return the print method, which has a return type of void. In other words the onError method must return a list.
So convert your statement to block body and return an empty list.
.onError((error, stackTrace) {
print(error.toString());
return [];
}