Want to get a list of names of all images in ‘assets’ directory in flutter? I’ve implemented the function below inside of a StatefullWidget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 contains all data about all assets that you add in pubspec.yaml
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.