easy_asset_generator 0.0.2
easy_asset_generator: ^0.0.2 copied to clipboard
A Flutter asset code generator that creates structured Dart accessors for asset folders.
Easy Asset Generator #
🚀 Stop writing asset paths manually! This package automatically generates type-safe asset classes for your Flutter project.
⚡ Quick Start #
1. Activate the Package #
dart pub global activate easy_asset_generator
2. Organize Your Assets #
your_project/
├── assets/
│ ├── images/ # Put your .png, .jpg files here
│ ├── icons/ # Put your .svg files here
│ ├── fonts/ # Put your .ttf files here
│ └── sounds/ # Put your .mp3 files here
├── lib/
└── pubspec.yaml
3. Run the Generator #
easy_asset_generator
4. Import & Use #
import 'package:your_app/resources/assets.dart';
// Use anywhere in your app:
Image.asset(Assets.images.imgLogo)
Text('Hello', style: TextStyle(fontFamily: Assets.fonts.fontRoboto))
🎯 What You Get #
Before (manual, error-prone):
Image.asset('assets/images/logo.png') // ❌ Typos possible
Image.asset('assets/images/3d.png') // ❌ Invalid identifier
Image.asset('assets/images/default.png') // ❌ Reserved keyword
After (generated, type-safe):
Image.asset(Assets.images.imgLogo) // ✅ Autocomplete
Image.asset(Assets.images.img3d) // ✅ Valid identifier
Image.asset(Assets.images.imgDefault) // ✅ No conflicts
📁 Generated Files #
After running the script:
lib/resources/
├── assets.dart # Main file - import this one
├── images.dart # All your image assets
├── icons.dart # All your icon assets
├── fonts.dart # All your font assets
└── sounds.dart # All your sound assets
🔧 Smart Naming #
The generator handles problematic file names automatically:
| Your File | Generated Name | Why |
|---|---|---|
logo.png |
imgLogo |
Images get img prefix |
home.svg |
svgHome |
SVGs get svg prefix |
roboto.ttf |
fontRoboto |
Fonts get font prefix |
3d.png |
img3d |
Handles files starting with numbers |
default.svg |
svgDefault |
Avoids Dart reserved keywords |
📋 Example Output #
Generated assets.dart:
// Auto-generated by EASY ASSET GENERATOR
// Do NOT modify this file manually!
import 'images.dart';
import 'icons.dart';
import 'fonts.dart';
class Assets {
static Images get images => Images.instance;
static Icons get icons => Icons.instance;
static Fonts get fonts => Fonts.instance;
}
Generated images.dart:
class Images {
static const String imgLogo = 'assets/images/logo.png';
static const String imgBackground = 'assets/images/background.jpg';
static const String img3d = 'assets/images/3d.png';
static const String imgDefault = 'assets/images/default.png';
}
🎨 Usage Examples #
// Images
Image.asset(Assets.images.imgLogo)
Image.asset(Assets.images.imgBackground)
// Icons
SvgPicture.asset(Assets.icons.svgHome)
SvgPicture.asset(Assets.icons.svgSettings)
// Fonts
Text(
'Custom Font',
style: TextStyle(fontFamily: Assets.fonts.fontRoboto),
)
// Container background
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(Assets.images.imgBackground),
fit: BoxFit.cover,
),
),
)
⚠️ Important Notes #
-
Add assets to pubspec.yaml:
flutter: assets: - assets/images/ - assets/icons/ - assets/fonts/ - assets/sounds/ -
Re-run after changes:
easy_asset_generator -
Assets must be in subfolders (not directly in
assets/)
🚨 Troubleshooting #
| Problem | Solution |
|---|---|
assets/ folder not found |
Create assets/ folder in project root |
No files found |
Put files in subfolders like assets/images/ |
Import errors |
Run flutter pub get after generation |
Assets not loading |
Check pubspec.yaml has asset declarations |
🎉 Benefits #
- ✅ No more typos in asset paths
- ✅ Autocomplete for all assets
- ✅ Compile-time safety - catch errors early
- ✅ Handles edge cases - numbers, keywords, special chars
- ✅ Organized by type - images, fonts, icons separate
- ✅ Single import -
import 'assets.dart'and you're done
Made with ❤️ by Easy Asset Generator
Turn asset management from a chore into a breeze! 🚀