1

With the release of the new dart:io libraries, in order to read the data from an HttpRequest we now need to:

Listen to the body to handle data and notified of it's completion.

In order to accomplish this, I've been using something similar to the following to get all of the data from the request (taken from the dart:io tests):

List<int> body = new List<int>();
request.listen(body.addAll, onDone: () {
  var str = new String.fromCharCodes(body);
  // Now do something with the string of data
});

Is there some way of using transform and/or StringDecoders to already provide a constructed result. The only way I can think of, I would still need to create a StringBuffer and use writeAll to ensure all data is passed in the event it doesn't all arrive at once, and still call the onDone before using the string in the buffer.

So I guess ultimately the question is: is there some way I can use a HttpRequest (or in all actuality any Stream) without needing to buffer/build the results and can just pass a callback or handler which receives the entire contents?

4

1 回答 1

0

我还没有尝试过,但我认为你应该能够使用 StringDecoder、toList() 和 join() 来获取整个身体:

request.transform(new StringDecoder()).toList().then((data) {
  var body = data.join('');
  print(body);
}
于 2013-02-26T20:38:15.617 回答