withOpenFile<R> function

Future<R> withOpenFile<R>(
  1. String pathToFile,
  2. R action(
    1. RandomAccessFile
    ), {
  3. FileMode fileMode = FileMode.writeOnlyAppend,
})

Opens a File and calls action passing in the open file. When action completes the file is closed. Use this method in preference to directly callling FileSync()

Implementation

Future<R> withOpenFile<R>(
  String pathToFile,
  R Function(RandomAccessFile) action, {
  FileMode fileMode = FileMode.writeOnlyAppend,
}) async {
  final raf = File(pathToFile).openSync(mode: fileMode);

  R result;
  try {
    result = action(raf);
  } finally {
    await raf.close();
  }
  return result;
}