Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf8.decoder not working after latest Flutter Upgrade

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;
  }
}
like image 380
blaze bnayak Avatar asked Apr 24 '26 02:04

blaze bnayak


1 Answers

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 of Stream.transform(StreamTransformer).
    • Example:
      • Before: foo.transform(utf8.decoder)...
      • After: utf8.decoder.bind(foo)...
like image 162
jamesdlin Avatar answered Apr 26 '26 14:04

jamesdlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!