utf8.decoder not working after latest Flutter Upgrade

4,000

Solution 1

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)...

Solution 2

Comment String reply = await utf8.decoder.bind(response).join();

and use the following code :

//String reply = await response.transform(utf8.decoder).join();
String reply;
request.close().then((response){
response.cast<List<int>>().transform(utf8.decoder).listen((content) {
    print (content);
    reply = content;
});
Share:
4,000
blaze bnayak
Author by

blaze bnayak

Updated on December 12, 2022

Comments

  • blaze bnayak
    blaze bnayak over 1 year

    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;
      }
    }
    
    • 10101010
      10101010 almost 5 years
      I guess it should work fine if you are on the stable channel
    • blaze bnayak
      blaze bnayak almost 5 years
      It was working fine just before the upgrades. Tried this solution stackoverflow.com/questions/56862020/… but it seems it is changing the expected response in some other string values
    • 10101010
      10101010 almost 5 years
      I did not get you. Does that solution not work you?
  • DomingoMG
    DomingoMG about 2 years
    This solution helped me thank you