forEachBlock method

Future<Null> forEachBlock(
  1. int blockSize,
  2. void action(
    1. Uint8List buffer
    )
)

Reads file by byte blocks and calls action for each block read.

This functions passes the byte buffer to the action function.

You can use this function for huge files.

Implementation

Future<Null> forEachBlock(
    int blockSize, void Function(Uint8List buffer) action) async {
  var raf = await open(mode: FileMode.read);
  while (true) {
    var buffer = await raf.read(blockSize);
    if (buffer.length == blockSize) {
      action(buffer);
    } else if (buffer.isNotEmpty) {
      action(buffer);
      break;
    } else {
      break;
    }
  }
  await raf.close();
}