我有100张图片需要在列表中显示.我不想硬编码所有的名字.
我怎么才能知道图片的名称呢?
我想要这样的代码:
final List<String> imageNames = await WhatEver.getNamesOfImagesInAssetsDirectory();
final widgets = imageNames.map((fileName) => Image.asset('images/${fileName}.png')).toList()
我有100张图片需要在列表中显示.我不想硬编码所有的名字.
我怎么才能知道图片的名称呢?
我想要这样的代码:
final List<String> imageNames = await WhatEver.getNamesOfImagesInAssetsDirectory();
final widgets = imageNames.map((fileName) => Image.asset('images/${fileName}.png')).toList()
我已经在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
包含您在pubspec.yaml
中添加的所有assets资源 的所有数据