readAsBytes method

Future<List<int>> readAsBytes(
  1. {void onBlobMetadata(
    1. BlobMetadata metadata
    )}
)

Reads the blob as bytes.

Optional callback onBlobMetadata, when non-null, will be invoked exactly once before the future is completed (unless an error occurs). The callback receives an instance BlobMetadata, which may contain metadata about the blob. For example, HTTP header often contains MIME type and length.

Implementation

Future<List<int>> readAsBytes({
  void Function(BlobMetadata metadata) onBlobMetadata,
}) async {
  final chunks = await read(
    onBlobMetadata: onBlobMetadata,
  ).toList();
  switch (chunks.length) {
    case 0:
      return List<int>(0);
    case 1:
      return chunks.single;
    default:
      final length = chunks.fold(0, (n, list) => n + list.length);
      final result = List<int>(length);
      var i = 0;
      for (var chunk in chunks) {
        result.setAll(i, chunk);
        i += chunk.length;
      }
      return result;
  }
}