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.
- Returns only List of Files.
Optional argument predicate
may be used to filter the Files.
Implementation
Future<List<File>> list_({
bool Function(File)? predicate,
}) async {
final completer = Completer();
final files = <File>[];
try {
Directory(clean(path)).list(recursive: true, followLinks: false).listen(
(event) {
if (event is File) {
final file = File(
event.path.substring(
event.path.startsWith(kWin32LocalPathPrefix)
? kWin32LocalPathPrefix.length
: 0,
),
);
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 [];
}
}