The class APIPostRequest was wroking all fine until a flutter upgrade hit and it shows an error of "The argument type 'Utf8Decoder' can't be assigned to the parameter type 'StreamTransformer'." while transforming HttpClientResponse's object into String using ...transform(utf8.decoder)...
class APIPostRequest {
Future<String> apiRequest(String url, Map jsonMap) async {
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
request.headers.set('Accept', 'application/json');
request.headers.set('Content-type', 'application/json');
request.headers
.set('Authorization', "Bearer " + UserConstants.userAccessToken);
request.add(utf8.encode(json.encode(jsonMap)));
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();
httpClient.close();
return reply;
}
}
See the corresponding breaking change announcement:
Error cases (and how to fix them):
If you see the following errors in your code, here's what you do to fix them:
- Error: "The argument type 'Utf8Decoder' can't be assigned to the parameter type 'StreamTransformer'."
- How to fix: Use
StreamTransformer.bind(Stream)instead ofStream.transform(StreamTransformer).- Example:
- Before:
foo.transform(utf8.decoder)...- After:
utf8.decoder.bind(foo)...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With