createTemp method

  1. @override
Future<Directory> createTemp([
  1. String? prefix
])

Creates a temporary directory in this directory.

Additional random characters are appended to prefix to produce a unique directory name. If prefix is missing or null, the empty string is used as prefix.

Returns a Future<Directory> that completes with the newly created temporary directory.

Implementation

@override
Future<file.Directory> createTemp([String? prefix]) {
  prefix ??= '';
  if (path == '') {
    throw ArgumentError('Directory.createTemp called with an empty path. '
        'To use the system temp directory, use Directory.systemTemp');
  }
  String fullPrefix;
  if (path.endsWith('/') || (Platform.isWindows && path.endsWith('\\'))) {
    fullPrefix = '$path$prefix';
  } else {
    fullPrefix = '$path${Platform.pathSeparator}$prefix';
  }

  final completer = Completer<Directory>();
  void callback(Object? err, result) {
    if (err == null) {
      completer.complete(Directory(result));
    } else {
      completer.completeError(err);
    }
  }

  var jsCallback = js.allowInterop(callback);

  fs.mkdtemp(fullPrefix, jsCallback);
  return completer.future;
}