Flutter,如何从图像文件夹中获取所有图像文件名?

Thi*_*rNL 5 flutter

我想将images/pets/ 中的所有图像文件名加载到List<String> animals. 我怎样才能做到这一点?

Ads*_*Han 7

path_provider可以得到目录temp和appDir目录

    final directory = await getApplicationDocumentsDirectory();
    String imagesDirectory = directory + "/images/pets/";
Run Code Online (Sandbox Code Playgroud)

之后就可以使用listSync方法查找这个目录下的文件了

final myDir = new Directory(imagesDirectory);
List<FileSystemEntity> _images;
_images = myDir.listSync(recursive: true, followLinks: false);
Run Code Online (Sandbox Code Playgroud)

我希望我在某种程度上有所帮助


Fil*_*cks 6

Flutter 生成一个名为 AssetManifest.json 的文件,您可以像从资产中读取普通文本文件一样读取默认包。

var manifestContent = DefaultAssetBundle.of(context).loadString('AssetManifest.json');
Run Code Online (Sandbox Code Playgroud)

读取并解析此文件,然后根据您需要的所有属性及其路径创建一个列表。只需仔细检查以确保您拥有正确的路径,这可能会在未来发生变化。在我看来,它就像一个占位符。

用于读取 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
Run Code Online (Sandbox Code Playgroud)