Pub Version

The assets_gen package helps you to generate a .dart file that contains all assets according to pubspec.yaml.

Way to reference asset pathSample Code
Use string path directlyImage.asset('assets/images/foo.png');❌ Unsafe
Use assets_genImage.asset(Assets.assets_images_foo_png);✅ Good

Getting Started


  assets_gen: any # Replace 'any' with version number.
  build_runner: any # Optional.


Call directly (Recommend)Use build_runner
Run a single build and exit.flutter pub run assets_gen buildflutter pub run build_runner build
Continuously run builds as you edit files.flutter pub run assets_gen watchflutter pub run build_runner watch

Note: Call assets_gen script directly will take effect on the target package and all it's dependencies.

More info about pub-run and build_runner.


# Specify an assets_gen section in pubspec.yaml
    - path/to/asset
optiontypedefault value
enablebooltrueEnable or not.
outputStringassets.dartOutput position, always under lib/.
class_nameStringAssetsThe generated class name.
gen_package_pathbooltrueWhether the builder should generate extra const variable with package info, e.g. packages/${package}/path/to/foo.png
ignore_resolutionbooltrueWhether the builder should ignore resolution variant. e.g. path/to/3.0x/foo.png will be ignored.
omit_path_levelsint0The path levels of generated key that the builder will omit. e.g. if levels is 2, the key of path/to/foo.png is foo_png.
excludeListnoneListed assets in exclude will be ignored in generated class. It supports glob syntax.
pluralsListnonePlurals support. e.g. Specify a plural - assets/vip/*.svg will generate a function like static String assets_vip_x_svg(Object p0) => 'assets/vip/${p0.toString()}.svg';.