built_value
8.13.0, released 3 months ago by dart.dev
Immutable value types with builders, enum classes and JSON serialization for Dart.
What it does
Provides a runtime library to define immutable data models using the builder pattern. Supports enum‑like classes and full JSON (de)serialization, working together with built_collection.
AI-written summary of version 8.13.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need strongly typed, immutable models with builders
- You require robust JSON serialization that handles generics and nested built collections
- You want enum‑style classes with additional behavior
Skip it when
- You prefer a lightweight solution without code generation
- Your project is small and a simple json_serializable approach is sufficient
Strengths
- Zero‑runtime reflection – fast and safe
- Full support for generics, nested built collections, and custom serializers
- Builder pattern enforces immutability and safe updates
Weaknesses
- Requires build_runner and a generator dev dependency
- Steeper learning curve compared to plain json_serializable
- Adds extra build step to the CI pipeline
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 4
Add built_value_generator and build_runner as dev_dependencies, then run `dart run build_runner build` (or watch).
abstract class Person implements Built<Person, PersonBuilder> {
String get name;
int get age;
Person._();
factory Person([void Function(PersonBuilder) updates]) = _$Person;
}
// Run `dart run build_runner build` to generate the *.g.dart file.Alternatives
Packages that do the same job and could replace built_value.
- freezed
Provides similar immutable data classes with union types and less boilerplate
96Compare - json_serializable
Simpler code‑gen for JSON (de)serialization without the builder pattern
94Compare
Works with
Packages commonly used together with built_value: ones it builds on, ones built on it, and its own family.
- built_collectionbuilt_value depends on itImmutable collections based on SDK collections with builder pattern.
- fixnumbuilt_value depends on itProvides 32‑ and 64‑bit signed fixed‑width integer types for Dart.
- platformSame familyCross‑platform abstraction for querying runtime platform info in Dart.
- asyncSame familyUtility classes and helpers that extend Dart's async library.
- hooksSame familyDart API for the JSON-based protocol for package build and link hooks.
- code_assetsSame familyProvides the hook protocol specification for bundling native code with Dart packages.
- httpSame familyA composable, Future‑based library for making HTTP requests across platforms.
- testSame familyA full featured library for writing and running Dart tests across platforms.