Sub topic 8
Adding Getters and Methods to a freezed Class
This chapter assumes you know Dart classes, named parameters, null safety, and how to run a terminal command in a Flutter project. You should also understand that freezed generates a second Dart file from your class declaration.
A freezed class can contain more than stored data. You can add computed getters and methods for behavior that belongs to that value. The important detail is the private constructor:
const Temperature._();This constructor gives the generated class a path to your custom members. Without it, a getter or method declared on the public factory may not be available on the generated object.
A computed getter
A getter is a property that calculates a value when you read it. It looks like a field, but it runs code.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'temperature.freezed.dart';
@freezed
abstract class Temperature with _$Temperature {
const Temperature._();
const factory Temperature({
required double celsius,
}) = _Temperature;
double get fahrenheit => celsius * 9 / 5 + 32;
}After code generation, callers can use the getter like a normal property:
const temperature = Temperature(celsius: 20);
print(temperature.fahrenheit); // 68.0The getter does not store a second fahrenheit value. It derives that value from the immutable celsius field. This keeps the model consistent because there is only one source of truth.
Adding a method
Use a method when the operation needs an argument or represents an action related to the value.
@freezed
abstract class Temperature with _$Temperature {
const Temperature._();
const factory Temperature({
required double celsius,
}) = _Temperature;
double get fahrenheit => celsius * 9 / 5 + 32;
bool isAbove(double limit) => celsius > limit;
Temperature convertedToCelsius() => Temperature(celsius: celsius);
}A method on an immutable model should usually return a new value rather than change the existing object. In practice, many updates are clearer with the generated copyWith method:
final warm = Temperature(celsius: 25);
final hotter = warm.copyWith(celsius: 30);The original warm object remains unchanged.
How the private constructor fits
The public factory creates the generated implementation, while the private constructor supplies the shared class body.
The private constructor must be const when the model supports constant construction. It should not have parameters. The factory remains responsible for declaring the model's fields.
Common mistakes
- Forgetting
const Temperature._()causes custom members to fail or behave unexpectedly. - Putting mutable fields in the class weakens the value-object design.
- Changing a field without using
copyWithbreaks immutability. - Editing the generated
.freezed.dartfile is temporary. Regeneration will overwrite it. - After changing the class, run the generator again:
dart run build_runner build --delete-conflicting-outputsGenerated code can then expose the getter and methods on the concrete object returned by the factory.
Exercise
Why does a freezed class use a private constructor when it has a custom getter or method?
Show answer
The private constructor lets the generated implementation inherit the custom members declared in the class body. The public factory still creates the generated implementation.