blaze bnayak
blaze bnayak

Reputation: 133

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;
  }
}

Upvotes: 2

Views: 5623

Answers (3)

Sukhi
Sukhi

Reputation: 14145

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;
});

Upvotes: 3

Gilmar Lima
Gilmar Lima

Reputation: 1

See below solution

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 utf8.decoder.bind(response).join();
        httpClient.close();
        return reply;
    }
}

Upvotes: 0

jamesdlin
jamesdlin

Reputation: 89995

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

Upvotes: 5

Related Questions