Flutter从资产文件夹读取所有文件 [英] Flutter read all files from asset folder

查看:1332
本文介绍了Flutter从资产文件夹读取所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资产文件夹,其中有一个tab文件夹,然后有一个文件夹列表,每个文件夹包含一些文件

I have an assets folder in which I have a tab folder and then a list of folders and each folder contains some files

现在,我想读取tabs文件夹中所有文件夹的名称以及tabs文件夹的每个子文件夹中所有文件的名称,即名为声音1,声音2,声音3的文件夹.....

Now I want to read the names of all the folders that are in the tabs folder and all the files that are in each subfolder of the tabs folder, i.e. folders that are named as sound 1, sound 2, sound 3 .....

简单来说,我想读取名称我的资产文件夹中的所有文件和文件夹。请任何人帮助。
谢谢,

In simple words, I want to read names of all files and folders that are in my assets folder. Please Anyone help. Thanks,

推荐答案

即使您仅在 pubspec.yaml中指定资产文件夹,则flutter编译器会在 AssetManifest.json 文件中列出这些文件夹中的所有文件。我们要做的就是读取此文件:

Even if you specified only asset folders in pubspec.yaml, the flutter compiler lists all files from these folders in AssetManifest.json file. All we have to do is read this file:

final manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final images = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/images'));

这篇关于Flutter从资产文件夹读取所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆