Here's my code that should return a json. I adapted this code from here https://github.com/flutter/flutter/issues/15110
Stream _defaultReturn(HttpClientResponse httpClientResponse) {
Stream response = httpClientResponse.
transform(utf8.decoder).
transform(json.decoder).
asyncMap((json) => jsonDecode(json));
return response;
}
Future<dynamic> get(String endpoint) async {
HttpClientRequest httpClientRequest =
await httpClient.getUrl(Uri.parse(_url + endpoint));
_addCookies(httpClientRequest);
final HttpClientResponse httpClientResponse =
await httpClientRequest.close();
return _defaultReturn(httpClientResponse);
}
I've put a return type of Stream
into _defaultReturn
because intellisense told me that giant thing returned me a Stream
. I would actually want to receive a json (which should be a map). I think I migth consume or subscribe to this stream to get something useful. However, I don't see parsin json as stream as being useful. Don't I need the entire json before parsing? Shouldn't I simply accumulate everything into a String
and then simply call jsonDecode
?
Which is the most efficient way of returning a json from an http call? And how to do it?