create method
Creates the file.
Returns a Future<File>
that completes with
the file when it has been created.
If recursive
is false
, the default, the file is created only if
all directories in its path already exist. If recursive
is true
, any
non-existing parent paths are created first.
If exclusive
is true
and to-be-created file already exists, this
operation completes the future with a PathExistsException.
If exclusive
is false
, existing files are left untouched by create.
Calling create on an existing file still might fail if there are
restrictive permissions on the file.
Completes the future with a FileSystemException if the operation fails.
Implementation
@override
Future<File> create({bool recursive = false, bool exclusive = false}) async {
if (exclusive && await exists()) {
throw file.FileSystemException('File already exists.');
}
if (recursive) {
await parent.create(recursive: true);
}
// write an empty file
final completer = Completer<File>();
void callback(Object? err, [fd]) {
if (err != null) {
completer.completeError(err);
} else {
fs.close(fd, js.allowInterop((err) {
if (err != null) {
completer.completeError(err);
} else {
completer.complete(this);
}
}));
}
}
final jsCallback = js.allowInterop(callback);
fs.open(_absolutePath, 'w', jsCallback);
return completer.future;
}