arrayBuffer method

Future<ArrayBuffer> arrayBuffer()

Returns a promise that resolves with an ArrayBuffer containing the entire contents of the Blob as binary data.

MDN Reference

Implementation

Future<ArrayBuffer> arrayBuffer() async {
  final buffer = _storage.$1;
  if (buffer != null) return buffer;

  final bytes = _storage.$2;
  if (bytes != null) {
    _storage = (bytes.buffer, bytes, _storage.$3);
    return bytes.buffer;
  }

  final chunks = <Uint8List>[];
  for (final part in _parts) {
    final bytes = switch (part) {
      (Blob blob, _, _, _) => (await blob.arrayBuffer()).asUint8List(),
      (_, ArrayBuffer arrayBuffer, _, _) => arrayBuffer.asUint8List(),
      (_, _, TypedData typedData, _) => typedData.buffer.asUint8List(),
      (_, _, _, String string) => string.bytes,
      _ => Uint8List(0),
    };
    chunks.add(bytes);
  }

  final newBytes = Uint8List.fromList(chunks.expand((e) => e).toList());
  _storage = (newBytes.buffer, newBytes, _storage.$3);

  return newBytes.buffer;
}