Package selection guidance compiled from public package information, community data, and project usage.
Flutter Gen Runner is a code generator for Flutter that automatically creates type-safe Dart code from assets, fonts, colors, and other resources. It eliminates the need for string-based references, reducing runtime errors and improving development speed. By generating constants and enums, it ensures type safety and enhances code maintainability. The tool integrates seamlessly into Flutter projects and supports multiple platforms including Android, iOS, macOS, Windows, and Linux.
Use cases
Generating type-safe asset references
Automating font and color constant creation
Reducing string-based API usage
Improving code reliability with compile-time checks
Streamlining UI development workflows
Key features
Type-safe resource generation
Supports assets, fonts, colors, and more
Compile-time error prevention
Seamless integration with Flutter
Cross-platform compatibility
Suitable for
Developers seeking type safety in UI resources
Teams aiming to reduce string-related bugs
Projects requiring scalable asset management
Flutter apps with extensive design systems
CI/CD pipelines needing automated code generation
Considerations
Requires setup in pubspec.yaml
Needs build runner execution
May increase initial project configuration
Not suitable for dynamic resource loading
Dependent on correct asset paths in configuration
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
Widgetbuild(BuildContext context) {
returnImage.asset('assets/images/profile.jpeg');
}
// The following assertion was thrown resolving an image codec:// Unable to load asset: assets/images/profile.jpeg
FlutterGen also supports dart pub workspaces.
Run build_runner from the workspace root, and make sure each workspace member
that should be built has resolution: workspace in its pubspec.yaml.
FlutterGen will resolve each package from the active build target instead of the
process working directory, so package-local pubspec.yaml configuration and
output paths continue to work in workspace builds.
If generated source files were removed manually while .dart_tool/build is
still present, run dart run build_runner clean from the workspace root before
running build --workspace again. The current post-process builder flow can
re-materialize files reliably after a clean build, but a warm incremental build
may skip unchanged manifests.
For workspace builds, use Dart >=3.7.0 together with build_runner >=2.12.0.
Use --workspace to treat the config file as a workspace root pubspec and run
generation for each listed workspace member. Without --workspace, the command
generates for exactly one package.
When --workspace is enabled, the root pubspec.yaml is used only to discover
workspace members from its workspace: section. FlutterGen then switches to
each member package and loads that package's own pubspec.yaml and, if
present, its local build.yaml.
That means workspace-wide command execution behaves like this:
Root pubspec.yaml: selects which packages to visit.
Member pubspec.yaml: provides flutter and flutter_gen configuration.
Member build.yaml: overrides generator options for that member only.
The --build option is only available in single-package mode. In workspace
mode, put any overrides in each package's local build.yaml instead.
Configuration file
FlutterGen generates dart files based on the key flutter and flutter_gen of pubspec.yaml.
Default configuration can be found here.
When using build_runner, you can also configure generator options in the
target build.yaml. These builder options are applied on top of the
pubspec.yaml configuration for the current target.
# build.yaml# ...targets:$default:builders:flutter_gen_runner:# or flutter_genoptions:output:lib/build_gen/# Optional (default: lib/gen/)line_length:120# Optional
# pubspec.yamlflutter:assets:-assets/images/-assets/images/chip3/chip.jpg-assets/images/chip4/chip.jpg-path:assets/images/icons/paint.svg-path:assets/images/icons/transformed.svgtransformers:-package:vector_graphics_compiler-assets/images/icons/dart@test.svg-assets/json/fruits.json-assets/flare/Penguin.flr-assets/rive/vehicles.riv-pictures/ocean_view.jpg# Also include assets from deferred components# https://docs.flutter.dev/perf/deferred-componentsdeferred-components:-name:myDeferredComponentassets:-assets/images/another_image.jps-assets/videos/a_large_video.mp4
These configurations will generate assets.gen.dart under the lib/gen/ directory by default.
Flavored assets
Flutter supports
Conditionally bundling assets based on flavor.
Assets are only available with flavors if specified.
flutter_gen will generate the specified flavors for assets regardless the current flavor.
The flavors field accessible though .flavors, for example:
You can use parse_animation to generate more animation details.
It will automatically parse all animation information for GIF and WebP files,
including frames, duration, etc. As this option significantly increases generation time,
The option is disabled by default; enabling it will significantly increase the generation elapse.
flutter_gen:images:parse_animation:true# <- Add this line (default: false)# This option implies parse_metadata: true when parsing images.
For GIF and WebP animation, several new nullable field is added to the
generated class. For example:
If you are using SVG images with flutter_svg you can use the integration feature. This feature also supports using vector_graphics_compiler transformer and the produced code will use the AssetBytesLoader for such transformed assets.
Note: For lottie integration with .lottie and .tgs files, you must add a custom decoder via decoder parameter, see lottie's document for more information.
In other cases, the asset is generated as String class.
// If don't use the Integrations.final svg = SvgPicture.asset(Assets.images.icons.paint);
final json = await rootBundle.loadString(Assets.json.fruits);
FlutterGen also support generating other style of Assets class:
Just follow the doc Use a custom font to specify fonts, then FlutterGen will generate related dart files.
No other specific configuration is required. Ignore duplicated.
FlutterGen can generate a Color class based on the name attribute and the color hex value.
If the element has the attribute type, then a specially color will be generated.
[SEVERE] flutter_gen_runner:flutter_gen_runner on $package$:
Bad state: No element
[SEVERE] Failed after 16.0s
The you most likely have a customized build.yaml to configure the build runner. In that case, all you have to do is to add the pubspec.yaml as build source to your build.yaml
targets:$default:sources:include:-pubspec.yaml# add this line-...