withOpenFile<R> function

R withOpenFile<R>(
  1. String pathToFile,
  2. R action(
    1. FileSync
    ), {
  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

R withOpenFile<R>(
  String pathToFile,
  R Function(FileSync) action, {
  FileMode fileMode = FileMode.writeOnlyAppend,
}) {
  final file = FileSync(pathToFile, fileMode: fileMode);

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