Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter, How to get all the image file name from images folder?

Tags:

flutter

I want to load all the image file names in images/pets/ to List<String> animals. How can I do that?

like image 965
ThitSarNL Avatar asked Jan 22 '19 15:01

ThitSarNL


People also ask

How do you find the file name of an image?

Right-click that file to open a context menu and then select "Properties." The Properties dialog box opens on the computer. The file name of the picture in question is listed to the right of Name.


2 Answers

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

like image 129
AdsHan Avatar answered Sep 23 '22 14:09

AdsHan


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
like image 35
Filled Stacks Avatar answered Sep 24 '22 14:09

Filled Stacks