readChunk method Null safety

Future<List<T>> readChunk(
  1. int size
)

Read next size elements from chunked stream, buffering to create a chunk with size elements.

This will read chunks from the underlying chunked stream until size elements have been buffered, or end-of-stream, then it returns the first size buffered elements.

If end-of-stream is encountered before size elements is read, this returns a list with fewer than size elements (indicating end-of-stream).

If the underlying stream throws, the stream is cancelled, the exception is propogated and further read operations will fail.

Throws, if another read operation is on-going.

Implementation

Future<List<T>> readChunk(int size) async {
  final result = <T>[];
  await for (final chunk in readStream(size)) {
    result.addAll(chunk);
  }
  return result;
}