copyWith and Value Equality
Update immutable data and compare by value.
Updating Immutable Data
You can not change an immutable object, so to update it you create a new one. The pattern that does this cleanly is copyWith.
What copyWith Does
A copyWith method returns a fresh object, keeping current values for fields you skip and replacing only the ones you pass.
final updated = user.copyWith(name: 'Ada');All lessons in this course
- Final Fields and Deep Immutability
- sealed Classes and Exhaustive switch
- base, interface, and final Modifiers
- copyWith and Value Equality