Sub topic 6
Changing Data with `copyWith`
Assume you know Dart classes, named parameters, null safety, and the basic setup of freezed with build_runner. This chapter shows how to change an immutable object without modifying the original object.
Why `copyWith` matters
An immutable object cannot change after it is created. This makes state updates easier to predict. Instead of changing a field in place, you create a new object containing the changed value.
A copyWith method is a generated method that copies an object and replaces selected fields. Fields that you do not provide keep their original values.
Using `copyWith`
Consider this freezed class:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'person.freezed.dart';
@freezed
abstract class Person with _$Person {
const factory Person({
required String name,
@Default(0) int age,
String? nickname,
}) = _Person;
}After running the generator, Person has a copyWith method. You can use it like this:
void main() {
const ada = Person(
name: 'Ada',
age: 36,
nickname: 'The Analyst',
);
final olderAda = ada.copyWith(age: 37);
final renamedAda = ada.copyWith(name: 'Augusta');
print(ada.age); // 36
print(olderAda.age); // 37
print(olderAda.name); // Ada
print(renamedAda.name); // Augusta
}The ada object is unchanged. Each call returns a separate Person value.
You can replace multiple fields in one call:
final updated = ada.copyWith(
age: 37,
nickname: 'Programmer',
);The generated method also preserves value equality. If no values change, the copied object has the same values as the original. It is still a separate object.
Nullable fields
A nullable field can contain a value or null. You can set it to a new value with copyWith:
final withNickname = ada.copyWith(nickname: 'Mathematician');Freezed's generated method also supports clearing a nullable field:
final withoutNickname = ada.copyWith(nickname: null);The generated implementation distinguishes between an omitted argument and an argument explicitly set to null. Omitting nickname keeps 'The Analyst'; passing null clears it.
Nested objects
For nested freezed objects, generated helpers can update a nested value without manually rebuilding every level.
@freezed
abstract class Address with _$Address {
const factory Address({required String city}) = _Address;
}
@freezed
abstract class User with _$User {
const factory User({
required String name,
required Address address,
}) = _User;
}
User moveToOslo(User user) {
return user.copyWith.address(city: 'Oslo');
}The nested update creates a new Address and a new User. The original objects remain unchanged.
Common mistakes
- Forgetting to run
build_runnerafter changing the class. - Assigning to a
finalfield instead of usingcopyWith. - Assuming
copyWithmutates the original object. - Treating a mutable list inside an immutable class as deeply immutable. The object reference is protected, but the list itself may still be changed unless you use an immutable collection or copy the list.
Exercise
Given const person = Person(name: 'Ada', age: 36);, write an expression that creates a person named Grace who is still 36 years old.
Show answer
final grace = person.copyWith(name: 'Grace');