assets_gen

Pub Version

The assets_gen package provides a builder 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

Install

dev_dependencies:
  build_runner: ">=1.0.0 < 2.0.0"
  assets_gen: ">=0.2.0 <1.0.0"

Usage

flutter pub run build_runner build: Run a single build and exit.

flutter pub run build_runner watch: Continuously run builds as you edit files.

More info about build_runner.

Options

Here are two ways to custom the generated file:

  • Offer an assets_gen_options.yaml file

  • # Specify an assets_gen section in pubspec.yaml
    flutter:
      assets:
        - path/to/asset
    assets_gen:
      - ...
    
optiontypedefault value
outputStringassets.g.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.

Libraries

assets_gen