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

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:

entities.forEach(print);

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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)