asset_gen
A Dart CLI tool to automatically generate a Dart file containing constants for all assets in your Flutter project.
โจ Features
- ๐ Recursively scans your
assets/directory - ๐งพ Generates a
lib/generated/assets.dartfile - ๐ Avoids hardcoded strings and typos
- ๐ค Great for code completion in IDEs
- โ Simple to use โ just one command!
๐ Getting Started
1. โ Activate the package globally:
dart pub global activate asset_gen
2. ๐ Project structure (example):
my_flutter_app/
โโโ assets/
โ โโโ images/
โ โ โโโ logo.png
โ โโโ icons/
โ โโโ user.png
โโโ lib/
3. โถ๏ธ Run the generator:
asset_gen
This will generate the file:
lib/generated/assets.dart
๐ฆ Output Example
Generated lib/generated/assets.dart:
// GENERATED CODE - DO NOT MODIFY BY HAND
class AppAssets {
static const String images_logo_png = 'assets/images/logo.png';
static const String icons_user_png = 'assets/icons/user.png';
}
๐ Usage in Code
import 'generated/assets.dart';
Image.asset(AppAssets.images_logo_png);
โ๏ธ Command Options
You can optionally specify a custom asset folder:
asset_gen my_custom_assets/
This will scan my_custom_assets/ instead of the default assets/ folder.
๐ก Why Use asset_gen?
- Prevent errors from typos in asset file paths
- Enable autocomplete for assets in your IDE
- Cleaner code โ no more
'assets/.../file.png'strings - Easy to regenerate when assets are added/removed
- Fits right into your build or CI pipeline
๐ License
Licensed under the MIT License. See the LICENSE file for details.
๐ Contributing
Contributions, issues, and feature requests are welcome!
- Fork this repo
- Create your feature branch:
git checkout -b feature/awesome-feature - Commit your changes:
git commit -m 'Add awesome feature' - Push to the branch:
git push origin feature/awesome-feature - Open a pull request
๐ Links
- ๐ฆ Pub.dev: https://pub.dev/packages/asset_gen
- ๐ป Dart SDK: https://dart.dev
- ๐ GitHub: https://github.com/yourusername/asset_gen
โค๏ธ Built for Flutter Developers
Save time, reduce bugs, and make your Flutter code cleaner with asset_gen.