10

I have images and I want to load them to my appplication. my_app/lib/... - here my source files my_app/assets/images/... - here my images

I need to get list of files in assets/images/ and after that show some of them (according to other logic) I'm trying this

final dir = Directory("assets/images/");
print(dir.existsSync());    // <---- it also print: false
var files = dir.listSync().toList();
files.forEach((e) => list.add(MyImageItem(e.path)));

The problem is: I recieve exception

FileSystemException: Directory listing failed, path = 'assets/images/'
(OS Error: No such file or directory, errno = 2)

I've tried different ways: assets/images/, images/, images and so on My pubspec.yaml

flutter:
  assets:
    - assets/images/
    - assets/

When I create Image directly all is fine

new Image(image: AssetImage("assets/images/cat.png"))

I knew that previously (month ago) each resource has to be declared in pubspec.yaml directly, but now assets/images/ is ok. I can load file by direct path. Why I can't access a directory? How to get list of files in directory to get them from my code?

Serge Breusov
  • 1,316
  • 4
  • 11
  • 24
  • Assets are not a directory. See https://docs.flutter.io/flutter/services/AssetBundle-class.html – Günter Zöchbauer Jun 21 '18 at 19:29
  • Ok, so how I have to solve my task? I need folder "images" somewhere and I need to get list of files in that folder. I tried AssetBundle also... working with files in flutter is contrintuitive to me... – Serge Breusov Jun 21 '18 at 21:43
  • @GünterZöchbauer Ok, so how I have to solve my task? I need folder "images" somewhere and I need to get list of files in that folder. I tried AssetBundle also... working with files in flutter is contrintuitive to me... – Serge Breusov Jun 21 '18 at 21:50
  • Did you find a solution to this yet? – Filled Stacks Jan 23 '19 at 03:43
  • Zip the image folders and unzip them into mobile disk space when launching app. File list can be done in disk space. – thundertrick Apr 01 '19 at 08:55

2 Answers2

2

When you add files to your assets, it means that you already know their paths. Store all the images paths in a list and access them whenever you need.

For example if you have 3 images in

your_app/assets/images/image1.jpg
your_app/assets/images/image2.jpg
your_app/assets/images/image3.jpg

create a list and store all of them like:

List<String> imagePaths = ['assets/images/image1.jpg', 'assets/images/image2.jpg', 'assets/images/image3.jpg'];

and for example if you want to access your first image, use

AssetImage(imagePaths[0])

Javid Noutash
  • 2,142
  • 17
  • 19
  • 1
    Thanks, I know this way, but it is a huge mess. I need 5-10k images and I don't like hard-code them all (even with generating code). Also I want to add new images over the time.... So, I need exactly list of files (or assets, or something like this) – Serge Breusov Jun 21 '18 at 23:10
  • 2
    Storing 5-10k images inside your app's package is a terrible idea, you better think of another solution. – Javid Noutash Jun 21 '18 at 23:51
  • 2
    If you must you can use code generation to generate a dart file with constants for each file. – Günter Zöchbauer Jun 22 '18 at 02:51
  • 1
    Storing 5-10k images calls for data base solution especially for mobile app. You should always think to make your app as small as possible as size without sacrificing functionality. – speedyGonzales Jul 05 '18 at 07:38
2

I keep a json file inside assets which records the file tree of assets folder.

When file list is needed, I just read from the json file.

Such json file can be easily generated by code.

However, this does not solve your problem directly when u have 5-10k images. Only one json file might be too large to read.

Images should be grouped and recorded in separated json files.

thundertrick
  • 1,634
  • 1
  • 17
  • 22