deleteDirectory method

  1. @override
Future<void> deleteDirectory(
  1. String path, {
  2. bool recursive = false,
})
override

Deletes the directory at path.

With recursive, its content goes with it. Without it, the directory must be empty. Throws when path is absent or, without recursive, still holds entries.

Implementation

@override
Future<void> deleteDirectory(String path, {bool recursive = false}) async {
  final String normalized = _normalize(path);
  if (normalized == _root) {
    throw StateError('the root directory cannot be deleted');
  }
  if (!_directories.contains(normalized)) {
    throw StateError('no directory at "$path"');
  }
  final List<String> descendants = _descendantsOf(normalized);
  if (!recursive && descendants.isNotEmpty) {
    throw StateError('directory "$path" is not empty');
  }
  _files.removeWhere(
    (String descendant, Uint8List bytes) => descendants.contains(descendant),
  );
  _directories.removeAll(descendants);
  _directories.remove(normalized);
}