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?
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.
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.
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.
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:
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/
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With