Sub topic 7
Defaults, Optional Fields, and Assertions in freezed
This chapter assumes you know Dart constructors, named parameters, null safety, and how to run build_runner. The goal is to choose the right field declaration for values that may be omitted, may be null, or must satisfy a rule.
Optional fields and defaults
A named parameter is optional when it does not use required. For a non-nullable field, an optional parameter needs a default value. Freezed provides the @Default annotation for this purpose.
A nullable field can be omitted without a default. Its value becomes null.
| Declaration | May be omitted? | Value when omitted |
|---|---|---|
| `required String name` | No | None |
| `String? nickname` | Yes | `null` |
| `@Default(0) int age` | Yes | `0` |
These declarations describe different meanings. Use String? when the absence of a value is meaningful. Use @Default when the object should always contain a usable value.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'person.freezed.dart';
@freezed
abstract class Person with _$Person {
@Assert('age >= 0')
const factory Person({
required String name,
@Default(0) int age,
String? nickname,
@Default(const <String>[]) List<String> tags,
}) = _Person;
}name must be supplied. age becomes 0 when it is omitted. nickname becomes null. tags gets an empty list.
Use a const collection for a default value. This makes the declaration a valid constant expression and avoids sharing a mutable list as a default object. Freezed also protects generated collection fields from accidental modification in many configurations, but your application should still treat model data as immutable.
Assertions for valid objects
An assertion checks a condition while an object is being created. In the example, age >= 0 rejects negative ages.
void main() {
const person = Person(name: 'Ada');
print(person.age); // 0
const invalid = Person(name: 'Ada', age: -1);
print(invalid);
}In a checked Dart run, the second construction throws an AssertionError. Assertions are useful for local invariants: rules that must always be true for a valid instance.
For external input, validate before constructing the model or use a validation result that represents failure. Do not rely on an assertion as a security or data-integrity boundary.
How construction works
The annotation processor reads the factory declaration and generates the concrete implementation. The generated constructor applies defaults and includes the assertion.
A common mistake is confusing omission with null. @Default(0) int age uses 0 when age is omitted, but age cannot explicitly receive null. In contrast, String? nickname accepts either a string or null.
When adding or changing a default, regenerate the files:
dart run build_runner build --delete-conflicting-outputsIf the generated constructor does not reflect your source, stale generated code is often the cause.
Practical decisions
- Mark identity or essential fields as
required. - Use nullable fields when “unknown” or “not provided” is a real state.
- Use
@Defaultfor a stable fallback value. - Use assertions for cheap, local invariants.
- Validate untrusted data separately from constructor assertions.
- Avoid mutable objects as defaults.
Exercise
What is the difference between @Default(0) int count and int? count when the caller omits the argument?
Show answer
count becomes 0 in the first declaration and null in the second.