Read HttpClientResponse into a String

Function Dart 1 revisions 706 2 years ago 2 years ago
Future<String> readResponse(HttpClientResponse response) {
  var completer = new Completer();
  var contents = new StringBuffer();
  response.transform(UTF8.decoder).listen((String data) {
  }, onDone: () => completer.complete(contents.toString()));
  return completer.future;

Simply pass it the HttpClientResponse you get from request.close() and it'd return a Future that eventually resolves to a String. (maybe use await here)

This requires a few obvious stuff:

import 'dart:async';
import 'dart:io';
import 'dart:convert';