HttpRequest
in Dart, is a Stream<List<int>>
, how to get the binary content as List<int>
?
I tried:
request.toList().then((data) {
// but data is a List<List<int>> !!!
});
It seems I should not use toList
. What's the correct method I should use?
We recently added a
BytesBuilder
available indart:io
. See here for more info.In your case the code could look like this:
The cool thing about using
BytesBuilder
is that it's optimized for large chunks of binary data. Note that I'm usingtakeBytes
to get the internal buffer of theBytesBuilder
, avoiding an extra copy.If you are using
dart:html
, you can useto get the response as one array buffer. You can access the buffer via
request.response
. But to access the bytes, you need to create aUint8List
represenation of the data:One way to do it is to set the
onDone
callback forlisten()
. The callback is fired when it has completed. Then just create a list of integers and add to it per every event:Alternatively, here's a one-liner:
I'll also add Anders Johnsen's great tip of using
BytesBuilder
class, which I think you should prefer: