dartdart-io

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


I would like to list all the contents of a directory (on the file system) using Dart. How can I do this?


Solution

  • The API has changed and I have updated the async code for M4 release (0.5.16_r23799 ):

    Future<List<FileSystemEntity>> dirContents(Directory dir) {
      var files = <FileSystemEntity>[];
      var completer = Completer<List<FileSystemEntity>>();
      var lister = dir.list(recursive: false);
      lister.listen ( 
          (file) => files.add(file),
          // should also register onError
          onDone:   () => completer.complete(files)
          );
      return completer.future;
    }