How do I list the contents of a directory with Dart? - Stack Overflow


Fri Jun 17 2022 08:07:23 GMT+0000 (UTC)

Saved by @X17

// How to list the contents of a directory in Dart
final dir = Directory('path/to/directory');
final List<FileSystemEntity> entities = await dir.list().toList();

//This creates a Directory from a path. Then it converts it to a list of FileSystemEntity, which can be a File, a Directory, or a Link. By default subdirectories are not listed recursively.

//If you want to print that list, then add this line:


//If you want to only get the files then you could do it like so:

final Iterable<File> files = entities.whereType<File>();