Skip to content
pubscout

code_builder

4.13.0, released 5 weeks ago by tools.dart.dev

A fluent, builder-based library for generating valid Dart code programmatically.

WASM-readyRock solid
$ flutter pub add code_builder
Source repository

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.

Works with

Packages commonly used together with code_builder: ones it builds on, ones built on it, and its own family.