0Pricing
Dart Academy · Lesson

copyWith and Value Equality

Update immutable data and compare by value.

copyWith and Value Equality is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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');

Writing copyWith

Each parameter is nullable and defaults to the existing field. This lets callers override just the fields they care about.

User copyWith({String? name}) =>
  User(name: name ?? this.name);

The Null Trap

The ?? this.field trick means you can not set a field to null with copyWith. Most data models accept this small limitation.

Default Equality

By default, two objects are equal only if they are the same instance. Two copies with identical data still count as different.

User('Ada') == User('Ada'); // false

Value Equality Goal

Often you want objects equal when their data matches, not their identity. That is called value equality, and you opt into it.

Override operator ==

Define operator == to compare the fields that matter. Now two objects with the same data are treated as equal.

bool operator ==(o) =>
  o is User && o.name == name;

Always Override hashCode

Whenever you override ==, you must also override hashCode. Equal objects must share a hash, or Sets and Maps misbehave.

int get hashCode => name.hashCode;

Object.hash Helper

Combine several fields cleanly with Object.hash. It produces one good hash code from all the values you pass in.

int get hashCode => Object.hash(name, age);

Equality Powers Collections

With value equality, a Set can drop true duplicates and a Map can find keys by their data, not their identity.

Let Tools Do It

Packages like equatable or freezed generate ==, hashCode, and copyWith for you, so your immutable models stay short and correct. 🛠️

Quick Check

Quick check on value equality.

Recap

You learned to update immutable data with copyWith and to compare by data using == plus hashCode for true value equality. 🌟

Frequently asked questions

Is the “copyWith and Value Equality” lesson free?

Yes — the full text of “copyWith and Value Equality” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “copyWith and Value Equality”?

Update immutable data and compare by value. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “copyWith and Value Equality” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Final Fields and Deep Immutability
  2. sealed Classes and Exhaustive switch
  3. base, interface, and final Modifiers
  4. copyWith and Value Equality
← Back to Dart Academy