Monorepo Toolkit
Asset Generator
The asset-gen command scans asset directories and generates type-safe Dart enum files. Instead of referencing assets by raw strings, you get compile-time-checked constants grouped by asset type.
icons/in packages/app_asset/assetstime/clock-stopwatch.svgclock.svghourglass.svg
/// Auto-generated icon assets.////// Generated from: assets/icons/timeenum AppTimeIcons( /// The asset filename. final String filename,) implements AppIconAsset { // … clockStopwatch('clock-stopwatch.svg'), clock('clock.svg'), hourglass('hourglass.svg'); static const _basePath = 'assets/icons/time'; // …}// A typo in the member name fails analyze.final path = AppTimeIcons.clock.path;// 'assets/icons/time/clock.svg'Usage
# Generate (the asset package is auto-discovered via its asset_generator.yaml)
dart run monorepo_toolkit asset-gen generate
# Preview without writing files
dart run monorepo_toolkit asset-gen generate --dry-run
# Specify a custom asset package path
dart run monorepo_toolkit asset-gen generate --package packages/my_assets
# Custom class name prefix
dart run monorepo_toolkit asset-gen generate --prefix MyFlags
| Flag | Abbr | Default | Description |
|---|---|---|---|
--dry-run | -d | false | Preview generated files without writing |
--package | -p | auto-discovered | Path to the asset package. When omitted, the workspace is scanned for an asset_generator.yaml |
--prefix | App | Prefix for generated class names |
How it works
The command scans the assets/ directory inside the target package, discovers all asset files, groups them by subdirectory, and generates one Dart enum file per group in lib/src/generated/.
For example, given this directory structure:
packages/app_asset/
assets/
icons/
ic_home.svg
ic_settings.svg
images/
bg_onboarding.png
logo.pngThe generator produces:
lib/src/generated/icons.dart-- enum withicHome,icSettingslib/src/generated/images.dart-- enum withbgOnboarding,logo
Lottie animations (.json and .lottie)
Files under assets/animations/ can be raw Lottie .json or .lottie (a dotLottie zip). Both become AppAssetType.lottie members, and both render through AppAssetWidget with no extra setup: Lottie.asset() detects the zip by its content.
Ship .lottie when you can. It is usually 85-90% smaller. You can keep the .json next to it as the design source:
assets/animations/
loading.json # design source, not generated
loading.lottie # generated: AppAnimations.loading -> loading.lottie
wifi.json # no .lottie twin: AppAnimations.wifi -> wifi.jsonWhen both files have the same base name, the generator uses the .lottie and skips the .json. Remove the .json from the package's flutter: assets: list if you do not want it in the app bundle.
Configuration
Place an asset_generator.yaml inside the asset package directory:
assets_path: assets
output_path: lib/src/generated
prefix: App
package_name: app_asset
asset_core_import: "package:asset_core/asset_core.dart"
icon_categories:
- outline
- filled
- bold| Field | Default | Description |
|---|---|---|
assets_path | assets | Directory to scan for assets (relative to package) |
output_path | lib/src/generated | Where to write generated files (relative to package) |
prefix | App | Prefix for generated class names |
package_name | app_asset | Package name used in asset path resolution |
asset_core_import | package:asset_core/asset_core.dart | Import path for the asset core library |
icon_categories | [] | Icon subdirectory categories to scan separately |
Melos shortcut
melos run asset-gen:generate # Generate asset enumsWorkflow
- Add asset files to
packages/app_asset/assets/<group>/ - Run
dart run monorepo_toolkit asset-gen generate - Import the generated enums in your feature code
- Access assets with compile-time safety:
AppIcons.icHome.path