最新Flutter升级后utf8.decoder无法正常工作

时间:2019-07-09 15:41:36

标签: android http flutter dart

APIPostRequest类一直运行得很好,直到升级失败为止,并且显示错误消息“无法将参数类型'Utf8Decoder'分配给参数类型'StreamTransformer'。”使用... transform(utf8.decoder)...将HttpClientResponse的对象转换为String时...

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

3 个答案:

答案 0 :(得分:0)

请参见corresponding breaking change announcement

  

错误情况(以及如何解决):

     

如果您在代码中看到以下错误,请执行以下操作来修复它们:

     
      
  • 错误:“不能将参数类型'Utf8Decoder'分配给参数类型'StreamTransformer'。”      
        
    • 如何解决:使用StreamTransformer.bind(Stream)代替Stream.transform(StreamTransformer)
    •   
    • 示例:      
          
      • 之前foo.transform(utf8.decoder)...
      •   
      • 之后utf8.decoder.bind(foo)...
      •   
    •   
  •   

答案 1 :(得分:0)

请参阅以下解决方案

num = len (price)
for i in range (num):
  print (price[i].text)

答案 2 :(得分:0)

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

并使用以下代码:

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