Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple assets in Flutter project

Tags:

flutter

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?

like image 907
rbasniak Avatar asked Mar 24 '18 17:03

rbasniak


People also ask

What are assets in flutter?

Common types of assets include static data (for example, JSON files), configuration files, icons, and images (JPEG, WebP, GIF, animated WebP/GIF, PNG, BMP, and WBMP). Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app.

How to add images to assets folder in flutter?

a) To add images, write the following code: flutter: assets: - assets/images/yourFirstImage.jpg - assets/image/yourSecondImage.jpg. b) If you want to include all the images of the assets folder then add this: flutter: assets: - assets/images/. Note: Take care of the indentation, assets should be properly indented to avoid any error.

What is a flutter project?

Most of the time when you create a Flutter project. You work with a single package. The project consists of a single pubspec. yaml, lib folder. You put all your features and utilities in the same package. But there are projects which break their features and utilities into multiple packages.

What is the best CLI tool for flutter project management?

M elos is a CLI tool for managing Flutter/Dart project with multiple packages. Melos is developed by a well-known team in the Flutter community i.e invertase. You can read in detail about Melos on their website but here is a quick list of features Melos provide:


3 Answers

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 files inside assets folder, its 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 folders inside assets ✅
assets:
  - assets/lottie/
  - assets/images/svg/
  - assets/images/png/
  - assets/audio/
like image 118
erluxman Avatar answered Oct 17 '22 20:10

erluxman


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/
like image 25
James Collins Avatar answered Oct 17 '22 22:10

James Collins


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.

like image 6
Günter Zöchbauer Avatar answered Oct 17 '22 21:10

Günter Zöchbauer