Flutter, How to get all the image file name from images folder?
8,033
Solution 1
path_provider you can get the directory the temp and appDir directory
final directory = await getApplicationDocumentsDirectory();
String imagesDirectory = directory + "/images/pets/";
After you can use the listSync method to find files of this Directory
final myDir = new Directory(imagesDirectory);
List<FileSystemEntity> _images;
_images = myDir.listSync(recursive: true, followLinks: false);
I hope that I have helped in some way
Solution 2
Flutter generates a file called AssetManifest.json which you can read up through the default bundle the same way you would read a normal text file from the assets.
var manifestContent = DefaultAssetBundle.of(context).loadString('AssetManifest.json');
Read and parse this file and then create a list from all the properties you need with their paths. Just double check to make sure you have the correct path, this can change in the future. It seems to me like a placeholder.
Pseudo-code for reading AssetManifest.json
var manifestContent = DefaultAssetBundle.of(context).loadString('AssetManifest.json');
var manifestMap = json.decode(manifestContent);
var imagePetPaths = manifestMap.keys.where((key) => key.contains('images/pets/'));
// You can either use the keys and fetch the value from the map,
or just use the key value since it's the same as the one in the pubspec.yaml
Author by
ThitSarNL
Updated on December 08, 2022Comments
-
ThitSarNL 28 minutes
I want to load all the image file names in images/pets/ to
List<String> animals
. How can I do that? -
ThitSarNL almost 4 yearsHi! @FilledStacks I dont't know how to do the way with AssetManifest.json. Can you show me some example or show helpful links?
-
Filled Stacks almost 4 yearsHi @YaungWal I don't have any examples or helpful links. Just read the json, decode it into a map using json.decode from the dart:convert package and then get all the keys that contains 'images/pets'. I'll add some pseudo-code in the answer above.
-
ThitSarNL almost 4 yearsThank you @FilledStacks I will try it.
-
Sachin over 2 years@FilledStacks the 'AssetManifest.json' file which you mentioned about will be autogenerated or should I have to create it manually? Also will you pls show the structue like what is contains and I want a short description with my every image
-
Filled Stacks over 2 years@Sachin You can check in your apk after it's built or in the build folder. it should be there in the flutter assets directore.
-
Kamlesh almost 2 yearsI want to list all images of /assets/images/wallpapers/ directory and store them in a List type variable to use further, how can i do?