write<T> method
Creates a sink to write path
, executes block
to write it, and then closes the sink.
This is a compact way to write a file.
Implementation
Future<T> write<T>(
String path,
FutureOr<T> Function(BufferedSink sink) block, {
FileMode mode = FileMode.write,
bool recursive = false,
}) {
return sink(path, mode: mode, recursive: recursive)
.buffered()
.then((e) => () async {
try {
return await block(e);
} finally {
try {
await e.close();
} catch (_) {}
}
}());
}