rbasniak
rbasniak

Reputation: 4954

Multiple assets in Flutter project

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

Answers (5)

erluxman
erluxman

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

Vinuka Vinnath
Vinuka Vinnath

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

James Collins
James Collins

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

YorkFish
YorkFish

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

Günter Zöchbauer
Günter Zöchbauer

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

Related Questions