21

I have 100 images that I need to display in a list. I don't want to hard code all names.

How can I get the names of the images?

I want to have code like this:

final List<String> imageNames = await WhatEver.getNamesOfImagesInAssetsDirectory();
final widgets = imageNames.map((fileName) => Image.asset('images/${fileName}.png')).toList()
Sergey
  • 1,608
  • 1
  • 27
  • 40
Kostya Vyrodov
  • 6,257
  • 5
  • 23
  • 34
  • 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 Jun 11 '19 at 14:31

3 Answers3

72

I've implemented the function below inside of a StatefulWidget

    Future _initImages() async {
      // >> To get paths you need these 2 lines
      final manifestContent = await rootBundle.loadString('AssetManifest.json');
    
      final Map<String, dynamic> manifestMap = json.decode(manifestContent);
      // >> To get paths you need these 2 lines

      final imagePaths = manifestMap.keys
          .where((String key) => key.contains('images/'))
          .where((String key) => key.contains('.svg'))
          .toList();
    
      setState(() {
        someImages = imagePaths;
      });
    }

AssetManifest.json contains all data about all assets that you add in pubspec.yaml

Vlad
  • 7,997
  • 3
  • 56
  • 43
Kostya Vyrodov
  • 6,257
  • 5
  • 23
  • 34
  • 16
    You can also use `final manifestContent = await rootBundle.loadString('AssetManifest.json');` instead of `DefaultAssetBundle` if you want to do this in a place where you don't have a `BuildContext`. – Javaxtreme Jul 21 '20 at 00:37
  • how to read and print image path from assets to console like ``` assets/image.jpg assets/image2.jpg ``` – perymerdeka Jun 25 '21 at 15:58
2

The 'AssetManifest.json' will soon be deprecated that will be replaced with 'AssetManifest.bin'.

However it's still undocumented I believe. If you're using a newer Flutter version (see the links for updates), there is an api to get the list of assets like so:

final assetManifest = await AssetManifest.loadFromAssetBundle(rootBundle);
// This returns a List<String> with all your images
final imageAssetsList = assetManifest.listAssets().where((string) => string.startsWith("assets/images/")).toList()

Tested on Flutter Channel stable, 3.10.6

Related links/github tracking:

Speed up first asset load by encoding asset manifest in binary rather than JSON

[tracking] Speed up asset manifest loading and parsing

A3tera
  • 21
  • 2
1
Future _initImages() async {
  // >> To get paths you need these 2 lines
  final manifestContent = await rootBundle.loadString('AssetManifest.json');

  final Map<String, dynamic> manifestMap = json.decode(manifestContent);
  // >> To get paths you need these 2 lines

  final imagePaths = manifestMap.keys
      .where((String key) => key.contains('images/'))
      .where((String key) => key.contains('.svg'))
      .toList();

  setState(() {
    someImages = imagePaths;//someimage is a list
  });
}