Reputation: 4954
According to documentation to include image assets in a Flutter project, I need to add them to the pubspec.yaml
like this:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
But I have ~900 images that I need to include in my application, do I really need to manually include one by one in the file?
Upvotes: 24
Views: 17221
Reputation: 19385
Sadly, you can't import the whole assets/ folder at once but you can now import all the assets inside a folder.
# β Don't Import every file inside assets folder, itβs tiring β
assets:
- assets/audio/celebrate.mp3
- assets/audio/splash.mp3
- assets/lottie/paper_plane.json
- assets/lottie/celebration.json
- assets/images/png/stats.png
- assets/images/alert.svg
# β Import all assets at once,π¨ Not allowed β
assets:
- assets/
# β
Just import every folder inside assets β
assets:
- assets/lottie/
- assets/images/svg/
- assets/images/png/
- assets/audio/
Upvotes: 50
Reputation: 81
We can add images or any asset by ending its directory by /
(Forward Slash).
Ex:
flutter:
assets:
- Images/cars/
- Images/animals/
It is a good habit not to add the whole "Images" directory at once.
Note:- Unlike any of the other programming languages, in YAML files we can't use the Asterisk sign ("*") to indicate a bunch of content.
Upvotes: 3
Reputation: 435
This is no longer a restriction. You can now include all assets in a directory.
In the flutter docs it states:
To include all assets under a directory, specify the directory name with the / character at the end:
flutter:
assets:
- directory/
- directory/subdirectory/
Upvotes: 36
Reputation: 87
The flutter development tool named Flr(Flutter-R) can help you to auto specify image/text/font assets in pubspec.yaml and generate r.g.dart file.
More details see: https://stackoverflow.com/a/61200175/1505549
Upvotes: 1
Reputation: 657118
Yes, currently this is required and a known limitation.
Wildcarding assets in pubspec.yaml (or allow an entire directory to be included) #4890
You could use code generation to get the assets listed all at once, but you'll also get into troubles when the list becomes too long.
Upvotes: 7