write_ method

Future<void> write_(
  1. dynamic content, {
  2. bool history = false,
})

Writes the content to the File.

Implementation

Future<void> write_(dynamic content, {bool history = false}) async {
  _locks[addPrefix(path)] ??= Lock();
  return _locks[addPrefix(path)]?.synchronized(() async {
    // Create the [File] if it does not exist.
    if (!await exists_()) {
      await create_();
    }
    try {
      final src = addPrefix(join(parent.path, '.${basename(path)}.src'));
      final dst = addPrefix(path);
      if (content is String) {
        await File(src).writeAsString(content, flush: true);
      } else if (content is Uint8List) {
        await File(src).writeAsBytes(content, flush: true);
      } else {
        throw FileSystemException(
          'Unsupported content type: ${content.runtimeType}',
          path,
        );
      }
      await File(src).rename_(dst);
    } catch (exception, stacktrace) {
      print(exception.toString());
      print(stacktrace.toString());
    }
  });
}