list_ method
Recursively lists all the Files present in the Directory.
- Safely handles long file-paths on Windows: https://github.com/dart-lang/sdk/issues/27825
- Does not terminate on errors e.g. an encounter of
Access Is Denied
. - Does not follow links.
Implementation
Future<List<File>> list_({bool Function(File)? predicate}) async {
final completer = Completer();
final files = <File>[];
try {
Directory(addPrefix(path))
.list(recursive: true, followLinks: false)
.listen(
(event) {
if (event is File) {
final file = File(removePrefix(event.path));
if (predicate?.call(file) ?? true) {
files.add(file);
}
}
},
onError: (error) {
// For debugging. In case any future error is reported by the users.
print(error.toString());
},
onDone: completer.complete,
);
await completer.future;
return files;
} catch (exception, stacktrace) {
print(exception.toString());
print(stacktrace.toString());
return [];
}
}