freezed
4.0.3, released 5 weeks ago by dash-overflow.net
Code generation for immutable classes, tagged unions, and cloning without boilerplate.
What it does
Freezed is a code generator for Dart that automatically generates boilerplate for immutable classes, such as constructors, copyWith methods, toString overrides, equality operators, and JSON serialization. It also supports union types, deep copying, and custom methods via a simple syntax.
AI-written summary of version 4.0.3, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to create complex immutable data models and state classes in Dart or Flutter.
- You want automatic support for copyWith, equality, hashCode, and toString without writing manual code.
- You are implementing union types or tagged data models.
Skip it when
- You prefer completely avoiding build_runner and code generation tools in your project workflow.
- You are working with simple mutable objects that do not require immutability or cloning.
Strengths
- Significantly reduces boilerplate code for data models.
- Supports union types, deep cloning, default values, and asserts.
- Integrates seamlessly with json_serializable for de/serialization.
Weaknesses
- Requires running a code generator (build_runner) during development.
- Can introduce steep debugging curves when generated files go out of sync.
Setup
- Kind
- Flutter widgets
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 11
Requires adding build_runner, freezed, and freezed_annotation to pubspec.yaml and running the build_runner watch command.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'main.freezed.dart';
part 'main.g.dart';
@freezed
abstract class Person with _$Person {
const factory Person({
required String firstName,
required String lastName,
required int age,
}) = _Person;
factory Person.fromJson(Map<String, Object?> json) => _$PersonFromJson(json);
}Alternatives
Packages that do the same job and could replace freezed.
- json_serializable
If you specifically need JSON parsing without the broader immutable data class features of Freezed.
94Compare
Works with
Packages commonly used together with freezed: ones it builds on, ones built on it, and its own family.
- analyzerfreezed depends on itPerforms static analysis of Dart code for tool integration and embedding.
- pub_semverfreezed depends on itVersions and version constraints implementing pub's versioning policy.
- dart_stylefreezed depends on itProvides an API and CLI to format Dart source code automatically.
- json_annotationfreezed depends on itProvides annotations for json_serializable to generate JSON (de)serialization code.
- buildfreezed depends on itPackage for authoring build_runner compatible code generators.
- build_configfreezed depends on itFormat definition and support for parsing build.yaml configuration.
- source_genfreezed depends on itSource code generation builders and utilities for the Dart build system.
- freezed_annotationfreezed depends on itAnnotations for the freezed code-generator.