put method

  1. @override
Future<bool> put(
  1. String path,
  2. dynamic contents, {
  3. Map<String, dynamic>? options,
})
override

Write the contents of a file.

Implementation

@override
Future<bool> put(
  String path,
  dynamic contents, {
  Map<String, dynamic>? options,
}) async {
  try {
    final file = fileSystem.file(path);

    if (contents is String) {
      await file.writeAsString(contents);
    } else if (contents is List<int>) {
      await file.writeAsBytes(contents);
    } else if (contents is Stream<List<int>>) {
      final bytes = await contents.fold<List<int>>(
        [],
        (previous, element) => previous..addAll(element),
      );
      await file.writeAsBytes(bytes);
    } else {
      throw ArgumentError(
        'Unsupported content type: ${contents.runtimeType}',
      );
    }

    _recordCreate(path);
    return true;
  } catch (e) {
    if (config.throw_) {
      throw UnableToWriteFileException(path, cause: e);
    }
    return false;
  }
}