Skip to content

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.

asset-genDrop in an SVG and get a compile-checked enum member

Step 1 of 4, before: Icons live in folders under packages/app_asset/assets/icons/. Each folder listed in icon_categories has its own enum, such as AppTimeIcons for the time folder.

  • icons/in packages/app_asset/assets
  • time/
  • clock-stopwatch.svg
  • clock.svg
  • hourglass.svg
packages/app_asset/lib/src/generated/icons/time_icons.dart
/// 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';  // …}
features/<feature>/lib/src/time_label.dart
// A typo in the member name fails analyze.final path = AppTimeIcons.clock.path;// 'assets/icons/time/clock.svg'

Usage ​

bash
# 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 My

Flags ​

FlagAbbrDefaultDescription
--dry-run-dfalsePreview generated files without writing
--package-pauto-discoveredPath to the asset package. When omitted, the workspace is scanned for an asset_generator.yaml
--prefixAppPrefix 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.png

The generator produces:

  • lib/src/generated/icons.dart -- enum with icHome, icSettings
  • lib/src/generated/images.dart -- enum with bgOnboarding, 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.json

When 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:

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:
  - outline
  - filled
  - bold
FieldDefaultDescription
assets_pathassetsDirectory to scan for assets (relative to package)
output_pathlib/src/generatedWhere to write generated files (relative to package)
prefixAppPrefix for generated class names
package_nameapp_assetPackage name used in asset path resolution
asset_core_importpackage:asset_core/asset_core.dartImport path for the asset core library
icon_categories[]Icon subdirectory categories to scan separately

Melos shortcut ​

bash
melos run asset-gen:generate   # Generate asset enums

Workflow ​

  1. Add asset files to packages/app_asset/assets/<group>/
  2. Run dart run monorepo_toolkit asset-gen generate
  3. Import the generated enums in your feature code
  4. Access assets with compile-time safety: AppIcons.icHome.path