withTempFileAsync<R> function

Future<R> withTempFileAsync<R>(
  1. Future<R> action(
    1. String tempFile
    ), {
  2. String? suffix,
  3. String? pathToTempDir,
  4. bool create = true,
  5. bool keep = false,
})

Creates a temp file and then calls action.

Once action completes the temporary file will be deleted.

The actions return value R is returned from the withTempFileAsync function.

If create is true (default true) then the temp file will be created. If create is false then just the name will be generated.

if pathToTempDir is passed then the file will be created in that directory otherwise the file will be created in the system temp directory.

The temp file name will be

Implementation

Future<R> withTempFileAsync<R>(
  Future<R> Function(String tempFile) action, {
  String? suffix,
  String? pathToTempDir,
  bool create = true,
  bool keep = false,
}) async {
  final tmp = createTempFilename(suffix: suffix, pathToTempDir: pathToTempDir);
  if (create) {
    touch(tmp, create: true);
  }

  R result;
  try {
    result = await action(tmp);
  } finally {
    if (exists(tmp) && !keep) {
      delete(tmp);
    }
  }
  return result;
}