Flutter read all files from asset folder

若如初见. 提交于 2020-07-17 11:21:14

问题


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

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,


回答1:


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'));



回答2:


The easiest way to do this is, open your pubspec.yaml and in the assets line, change it as given below

assets:
    - assets/*

This will add all the folders and files in the asset folders.




回答3:


in pubspec.yaml file:

add your files with full paths under the assets:

  assets:
    - assets/tabs/sound1/sample1.mp3
    - assets/tabs/sound1/sample2.mp3

and so on, then access them in your project.



来源:https://stackoverflow.com/questions/58740104/flutter-read-all-files-from-asset-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!