readFileAsStream method

  1. @override
Stream<Uint8List> readFileAsStream(
  1. String filePath, {
  2. int chunkSize = 8192,
})
override

Reads a file in chunks and returns a stream of bytes chunkSize determines the size of each chunk (default 8KB)

Implementation

@override
Stream<Uint8List> readFileAsStream(String filePath,
    {int chunkSize = 8192}) async* {
  final file = await _getFile(filePath);
  if (file == null) {
    throw Exception('No file selected or file not found');
  }

  int offset = 0;
  final int totalSize = file.size;

  while (offset < totalSize) {
    final int end =
        (offset + chunkSize < totalSize) ? offset + chunkSize : totalSize;

    // Read chunk as blob slice
    final blob = file.slice(offset, end);
    final bytes = await _blobToBytes(blob);

    yield bytes;
    offset = end;
  }
}