Sub topic 13
Updating Nested Objects
This chapter assumes you know Dart named parameters, null safety, and basic freezed classes. It also assumes that code generation is configured with build_runner.
A freezed object is immutable. After you create it, you cannot change one of its fields directly. Instead, copyWith creates a new object with selected values replaced.
That works naturally for a single level:
final olderUser = user.copyWith(name: 'Grace');Nested data needs one extra step. Suppose a User contains an Address. To change the city, you could copy the address first and then copy the user:
final movedUser = user.copyWith(
address: user.address.copyWith(city: 'Oslo'),
);freezed also generates a nested copyWith helper. The shorter form is:
final movedUser = user.copyWith.address(city: 'Oslo');Defining nested models
Each nested model must be a freezed class with its own generated part file.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'user.freezed.dart';
@freezed
abstract class Address with _$Address {
const factory Address({
required String city,
required String country,
}) = _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');
}
void main() {
const user = User(
name: 'Ada',
address: Address(city: 'London', country: 'United Kingdom'),
);
final moved = moveToOslo(user);
print(user.address.city); // London
print(moved.address.city); // Oslo
print(moved.address.country); // United Kingdom
}The generated helper changes only the requested nested field. The original user and its original address remain unchanged. This makes state transitions easier to reason about:
flowchart TD
A[Original User] --> B[Original Address]
A --> C[copyWith.address city Oslo]
C --> D[New User]
D --> E[New Address with city Oslo]
B --> F[Original Address remains unchanged]Why this matters in Flutter
Flutter state often contains several levels of data. For example, a screen state might contain a user, the user might contain an address, and the address might contain a country code.
With mutable objects, a deeply nested update can change data without clearly showing which state transition occurred. With immutable objects, every update produces a new value:
state = state.copyWith.user.address(city: 'Oslo');A state-management library can then compare the old and new state and notify the correct listeners.
Nullable nested fields
A nullable nested field needs special attention:
@freezed
abstract class Profile with _$Profile {
const factory Profile({
required String username,
Address? address,
}) = _Profile;
}When address is null, there is no object to update. Code such as profile.copyWith.address(city: 'Oslo') cannot create an address automatically. Create one explicitly:
final withAddress = profile.copyWith(
address: const Address(
city: 'Oslo',
country: 'Norway',
),
);Collections are a separate concern
Nested copyWith protects the model references, but it does not make every collection deeply immutable. A List<String> can still be changed if code keeps a mutable reference to it. Consider using immutable collection types or copying lists at the boundary where data enters your model.
Exercise
What is the difference between user.copyWith(address: user.address.copyWith(city: 'Oslo')) and user.copyWith.address(city: 'Oslo')?
Show answer
Both create a new user with a new address whose city is Oslo. The second form uses the nested copyWith helper generated by freezed and avoids manually copying the address.