我想在dart中将响应作为字符串检索,我指的是以下代码:

  Future<String> readResponse(HttpClientResponse response) {
    var completer = new Completer();
    var contents = new StringBuffer();
    response.transform(utf8.decoder).listen((data) {
      contents.write(data);
    }, onDone: () => completer.complete(contents.toString()));
    return completer.future;
  }

我从这个链接获得引用:
Retrieving the response body from an HttpClientResponse

但这给我一个错误:



我正在学习dart,但无法解决问题,我使用的是Dart 2.7

最佳答案

您必须指定Completer的类型。

import 'dart:async';

Future<String> readResponse(HttpClientResponse response) {
  var completer = Completer<String>();
  var contents = StringBuffer();
  response.transform(utf8.decoder).listen((data) {
    contents.write(data);
  }, onDone: () => completer.complete(contents.toString()));
  return completer.future;
}

08-27 02:03