delete_ method
Deletes the FileSystemEntity.
Implementation
Future<void> delete_() async {
// Surround with try/catch instead of using [Directory.exists_],
// because it confuses Windows into saying:
// "The process cannot access the file because it is being used by another process."
try {
if (this is File) {
await File(clean(path)).delete(recursive: true);
} else if (this is Directory) {
final directory = Directory(clean(path));
// TODO: [Directory.delete] is not working with `recursive` as `true`.
// Bug in [dart-lang/sdk](https://github.com/dart-lang/sdk/issues/38148).
// Adding a workaround for now.
if (await directory.exists_()) {
final contents = await directory.list_();
await Future.wait(
contents.map((file) => file.delete_()),
);
}
await directory.delete(recursive: true);
}
} catch (exception, stacktrace) {
print(exception.toString());
print(stacktrace.toString());
}
}