Skip to content
Back to archipelago.banuacoder.com

DevTools

How to generate type-safe asset enums in Flutter ​

Free Reference for asset_generator: every variable, option and package it generates. Command: archipelago generate asset_generator

Add the asset-gen command, write asset_generator.yaml, generate typed enums from your asset folders, and use them in widgets.

What you'll learn ​

  • Adding the asset-gen command to your monorepo toolkit
  • Writing the asset_generator.yaml config
  • Generating typed asset enums from your asset folders
  • Using the enums in widgets

Prerequisites ​

Step 1: Generate the Asset Generator Command ​

New projects already include this brick. Run it on an older project:

bash
archipelago generate asset_generator

The prompts are:

  • appName: the name of your app
  • isForMonorepo: leave this as true

Or use a config file:

json
{
  "appName": "MyApp",
  "isForMonorepo": true
}
bash
archipelago generate asset_generator --config asset_gen_config.json

Step 2: Understand the Generated Structure ​

The brick adds a script package and registers AssetGenCommand in the toolkit.

text
devtools/
└── scripts/
    └── asset_generator/
        ├── example/asset_generator.yaml    # Config schema reference
        └── lib/src/
            ├── commands/                   # asset-gen and its generate subcommand
            ├── models/
            └── services/asset_scanner_service.dart

Step 3: Write the Config ​

Put asset_generator.yaml next to the pubspec.yaml of the package that owns your assets. Copy devtools/scripts/asset_generator/example/asset_generator.yaml as a start.

yaml
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: [actions, arrows, communication]

Set prefix to the prefix you used for asset_core. A mismatch produces code that points at types that do not exist.

Step 4: Generate the Enums ​

bash
dart run monorepo_toolkit asset-gen generate

The command finds the one package that has an asset_generator.yaml. Pass --package packages/app_asset when there is more than one. Pass --dry-run to preview.

Each category becomes an enum. Icons land in lib/src/generated/icons/<category>_icons.dart. Other categories stay flat. A generated.dart barrel exports everything.

Some details:

  • Lottie .json and .lottie files both work. If both exist for one name, the enum points at the .lottie.
  • A _dark file next to an asset becomes its darkPath.
  • .riv files are skipped with a warning.

Step 5: Use the Enums ​

dart
// Before: a string that can drift
SvgPicture.asset('assets/icons/actions/copy.svg');

// After: a typed value
const icon = AppActionsIcons.copy;

Use the UI kit asset widgets to load an enum value. See Flutter UI Kit.

Key Customization Points ​

CustomizationWhere to Change
Asset folder and output folderassets_path and output_path in asset_generator.yaml
Icon categoriesicon_categories in asset_generator.yaml
Class prefixprefix in asset_generator.yaml

Next Steps ​