我需要在列表中显示100张图像。我不想对所有名称进行硬编码。
如何获得图像名称?
我想要这样的代码:
final List<String> imageNames = await WhatEver.getNamesOfImagesInAssetsDirectory(); final widgets = imageNames.map((fileName) => Image.asset('images/${fileName}.png')).toList()
我已经实现了下面的功能 StatefullWidget
StatefullWidget
Future _initImages() async { // >> To get paths you need these 2 lines final manifestContent = await DefaultAssetBundle.of(context).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 包含有关您添加的所有资产的所有数据 pubspec.yaml
AssetManifest.json
pubspec.yaml