0

What I want to do is get a List of all files in a directory. I have my Directory, and it works. At other places in my code I access files contained in those directories. However, when I try to add those files to a list, it gives me following error

E/flutter ( 4721): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Directory listing failed, path = 'assets/movies/apocalypse_now/' (OS Error: No such file or directory, errno = 2)
E/flutter ( 4721): null

I call the method with the following first line of code, the second one is to test if I can access files in the directory, which i can.

Text(getDirContents(new Directory('assets/movies/apocalypse_now')).toString()),
Image.asset("assets/movies/apocalypse_now/poster.png", width: 90, height: 90),

After running throu this, my app there shows

[] (the poster of apocalypse now)

I did this in order to see if this works, this is not how things will later look.

The method with which i try to get the files looks like this:

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

I don't know why this is not working or what i need to fix, but i tried around and got nothing to work.

Maritn Ge
  • 997
  • 8
  • 35
  • Assets isn't a directory. There's an answer on SO showing how to list the contents of the assets bundle. You should be able to find out through search. On mobile; will look later. – Richard Heap Nov 14 '19 at 10:06
  • @RichardHeap i thought a directory is just basically a path to a folder/ a folder? or am i just wrong on that regard? – Maritn Ge Nov 14 '19 at 10:35
  • Possible duplicate of [How do I get an array/list filled with all the image paths I loaded as assets in Flutter?](https://stackoverflow.com/questions/56369100/how-do-i-get-an-array-list-filled-with-all-the-image-paths-i-loaded-as-assets-in) – Richard Heap Nov 14 '19 at 11:04
  • That's correct, but assets isn't a folder. The contents of assets gets bundled as part of the build. `Image.asset()` knows how to extract the asset from the bundle. – Richard Heap Nov 14 '19 at 11:06
  • It is, in theory, a duplicate, but does that work with every directory? because assets is a folder. at least on my pc it is (C:\Users\xxx\dev\flutter_testapp2\assets). Or does this not get treated as a folder – Maritn Ge Nov 14 '19 at 11:10
  • In your source project, assets is a folder. During the build, your assets get bundled up into something that's not a directory structure - think perhaps of a zip file or similar. When you app is running it has access to that bundle, but it's not a directory structure or files. You use special methods like `Image.asset` or `RootBundle.load` to extract the contents. The linked question shows how to list the contents of the bundle. – Richard Heap Nov 14 '19 at 11:15
  • i used a different solution now to fix this, can i close it? should i delete it? – Maritn Ge Nov 14 '19 at 11:18

0 Answers0