heisenberg
heisenberg

Reputation: 1

Creating a directory list in windows using Dart

I am trying to create a directory list on windows using dart but I get an error

Here is my code

void listerine(Directory pat){
  //Directory root = new Directory(pat);
  //var root = Directory.systemTemp;
  pat.list(recursive: true, followLinks: false).listen((FileSystemEntity entity){

main(List<String> arguments){

  var root = new Directory("C:\\");

  print(root);
  listerine(root);

}

Btw I am using android studio as an IDE.

I gives me this error.

I/flutter ( 7224): Directory: 'C:\'
E/flutter ( 7224): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7224): FileSystemException: Directory listing failed, path = 'C:\/' (OS Error: No such file or directory, errno = 2)
E/flutter ( 7224): #0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1114:29)
E/flutter ( 7224): #1      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7224): #2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

Upvotes: 0

Views: 1231

Answers (1)

Richard Heap
Richard Heap

Reputation: 51732

You have the question tagged as flutter but seems you are asking about a pure Dart program?

This code

import 'dart:io';

void listerine(Directory pat) async {
  await for (var v in pat.list()) {
    print(v);
  }
}

main() {
  listerine(new Directory("C:\\"));
}

produces what you'd expect on Windows, namely

Directory: 'C:\$Recycle.Bin'
Directory: 'C:\$WINDOWS.~BT'
Directory: 'C:\Apps'
...

Flutter uses a modified version of the Dart SDK replacing html support with the mobile rendering engine (skia).

The majority of Dart code can run in both versions, but not all. This means that you can re-use packages in Dart VM applications and in Flutter applications - but again not all. In particular, interacting with the browser or the platform are only available in their respective versions.

To interact with the Windows file system, you need the Dart VM version. Download this separately. As Günter says, to interact with the mobile file system in Flutter, use path_provider.

Upvotes: 1

Related Questions