How can I get a list of filepaths from the project assets in Flutter?

Issue

I have added a bunch of Images to my App’s resources folder and I want to list all of the Images from my icons folder, so the user can pick which one they want to use for their listelement.

My Folder is included in the Pubspec.yaml and I can call AssetImage("/res/assets/icons/") and it gets the image I want manually.

I want to store the path later to call AssetImage(path) on it.

But when I call List files = Directory("/res/assets/icons/").listSync();, I get this exception:

Exception

What do I need to do to get all the file paths from the files in my directory?

Solution

you can list all the images in your specific directory like this:

final images = json.decode(await rootBundle.loadString('AssetManifest.json')).keys
    .where((String key) => key.contains('res/assets/icons/'))
    .toList();
print(images.toString()); 

Answered By – tareq albeesh

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *