Flutter read all files from asset folder
Asked Answered
M

5

18

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

enter image description here

I want to read the names of all the files and folders that are in my assets folder. How do I do that?

Mezzanine answered 6/11, 2019 at 23:58 Comment(6)
Hi, what have you tried so far?Turmel
Hi, I have tried using path_provider library but it don't work. It throughs exceptions. I looked up in AssetManifest.json as I read somewhere that this file contains information about every asset but it seems like it only contains information that we enter in pubspec.yaml. I don't want to add all file paths to pubspec.yaml. I added assets: - assets/ to get all files available for use in my code. Now I need each file path in a list or map without hard coding them in my code, so that I can iterate through them and show them in a ListView. ThanksMezzanine
Have you solved this problem? I met same matter as youWalburga
I have to hard code the strings. but I haven't tried @Spatz answer. You can try that.Mezzanine
I want to list all images of /assets/images/wallpapers/ directory and store them in a List type variable to use further, how can i do?Handicraftsman
Future myGetAssetImages() async { final manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json'); List<String> images = json.decode(manifestJson).keys.where((String key)=>key.startsWith('assets/images/wallpapers')).toList(); return images; } -- From @Spatz answer. It has worked for meAec
T
27

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'));
Titustityus answered 4/5, 2020 at 10:54 Comment(3)
I tried that but it seems like this file contains information that we enter in pubspec.yaml. So, I don't want to enter all asset names in the manifest.Mezzanine
@Taha Malik You have to place only folder names (with trailing slash) in pubspec.yaml, not an asset file names.Titustityus
can use by this way without context final manifestJson = await rootBundle.loadString('AssetManifest.json')Esp
P
1

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

assets:
  - assets/

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

Politburo answered 7/11, 2019 at 7:13 Comment(1)
I was doing : assets: - assets/ it get all the files without * as well. This only allows an asset to be used in your app. Still we have to know each file and directory name and hard code them into our code. What I want is I want an iterative solution i.e. i don't know how many files or folders are there in assets folder just get all file paths and show all files in a list.Mezzanine
A
1

I've got same issue,

As of sep 2021, there is still no way to get all the assets without manually listing it in pubspec.yaml before calling

final manifestJson = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');

However there is workaround, if you are using Android Studio/IntelliJ You can install https://plugins.jetbrains.com/plugin/13629-flutter-assets-ref-generator

Then you can use it to list all your assets files into pubspec.yaml with one click of button.

It solve the problem for me.

Anthroposophy answered 20/9, 2021 at 6:5 Comment(0)
F
0

According to the Flutter Official, they now provide the common way to insert all the files in the directories.

flutter:
  assets:
    - directory/
    - directory/sub_directory/

ref : https://docs.flutter.dev/development/ui/assets-and-images

Firstborn answered 10/12, 2021 at 7:49 Comment(0)
C
0

Spatz's answer is correct, however you don't always have context available.

In those cases, you can use rootBundle:

final manifestJson = await rootBundle.loadString('AssetManifest.json');
final images = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/images'));
Complexioned answered 20/11, 2023 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.