readAsBytes method

Future<List<int>> readAsBytes({
  1. int? maxSize,
})

Reads the entire file content as bytes.

If maxSize is provided and the file exceeds it, a StateError is thrown instead of loading the file into memory.

💣 Warning: Calling this without a maxSize on very large files can cause memory issues.

Implementation

Future<List<int>> readAsBytes({int? maxSize}) async {
  if (_deleted) throw StateError('Cannot read: file has already been deleted or moved.');
  if (maxSize != null && size > maxSize) {
    throw StateError(
      'File "$filename" is $size bytes, which exceeds the maxSize limit of $maxSize bytes. '
      'Use openRead() for streaming access instead.'
    );
  }
  return File(tempPath).readAsBytes();
}