code_builder
4.13.0, released 5 weeks ago by tools.dart.dev
A fluent, builder-based library for generating valid Dart code programmatically.
What it does
Provides a fluent, builder-based API to construct Dart syntax structures like classes, methods, and expressions. Uses emitters and allocators to generate formatted, valid Dart code with automatic symbol scoping.
AI-written summary of version 4.13.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You are building custom code generators or developer tools that output Dart source files.
- You need programmatic control over AST construction and automatic import prefixing.
Skip it when
- You just need runtime immutability or basic JSON serialization without building custom code generators.
- You prefer writing raw string templates over programmatic AST builders.
Strengths
- Fluent builder-based API makes constructing complex Dart structures intuitive.
- Automatic scoping prevents symbol conflicts by handling import prefixes dynamically.
Weaknesses
- Requires manual formatting via external packages like dart_style to ensure clean output.
- Steeper learning curve compared to simple string interpolation for basic tasks.
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 5
Add the package to pubspec.yaml dependencies and import it where code generation takes place.
import 'package:code_builder/code_builder.dart';
void main() {
final animal = Class((builder) => builder
..name = 'Animal'
..extend = refer('Organism')
);
}Alternatives
Packages that do the same job and could replace code_builder.
- copy_with_extension
Annotation for generating copyWith extensions code using copy_with_extension_gen.
87Compare - build_version
Generate a Dart file with the package version at build time.
77Compare - source_gen
Source code generation builders and utilities for the Dart build system.
84Compare - source_helper
Utilities to help with Dart source code generation and string literal formatting.
81Compare
Works with
Packages commonly used together with code_builder: ones it builds on, ones built on it, and its own family.
- built_valuecode_builder depends on itImmutable value types with builders, enum classes and JSON serialization for Dart.
- matchercode_builder depends on itSupport for specifying test expectations via an extensible Matcher class.
- built_collectioncode_builder depends on itImmutable collections based on SDK collections with builder pattern.
- mockitoBuilt on code_builderA mock framework inspired by Mockito with APIs for Fakes, Mocks, behavior verification, and stubbing.
- pigeonBuilt on code_builderType-safe code generator for communication between Flutter and host platforms.
- retrofit_generatorBuilt on code_builderGenerates Dio client code from Retrofit‑style annotations.
- injectable_generatorBuilt on code_builderCode generator that creates GetIt registrations for the injectable library
- auto_route_generatorBuilt on code_builderCode generator for the auto_route declarative routing solution.