0Pricing
Dart Academy · Lesson

Destructuring With Patterns

Pull apart records, lists, and maps.

Destructuring With Patterns is a free Dart Academy lesson on CoddyKit — lesson 3 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.

What Is Destructuring?

Destructuring means pulling values out of a structure and into variables in one move. It is unpacking a box and labeling each item at once. 🎁

Patterns Describe Shape

A pattern is a template that matches the shape of a value. If the value fits, Dart binds its pieces to the names you wrote.

Destructure a Record

Put a record pattern on the left of = and Dart fills each variable from the matching position. Two values, two names, instantly.

var (a, b) = (1, 2);
print(a);

Named Record Patterns

For named fields, match by name inside the pattern. The field name on the left tells Dart which value to grab.

var (x: a, y: b) = (x: 3, y: 7);
print(b);

Swapping Made Easy

Destructuring shines for swapping two variables. Build a record, unpack it, and the values trade places in one clean line.

(a, b) = (b, a);
print(a);

Destructure a List

A list pattern uses square brackets. Each slot binds one element, so positions line up with your variables.

var [first, second] = [10, 20];
print(first);

The Rest Element

Inside a list pattern, ... captures any leftover elements. Grab the head and let the rest collect the tail.

var [head, ...rest] = [1, 2, 3, 4];
print(rest);

Destructure a Map

A map pattern reads values by key. List the keys you care about and bind each to a variable.

var {'id': id} = {'id': 42, 'name': 'A'};
print(id);

The Wildcard Pattern

Use an underscore as a wildcard when a slot does not interest you. It matches anything and binds nothing.

var (_, important) = ('skip', 99);
print(important);

Destructuring in for-in

You can destructure right inside a for-in loop. Unpack each record as you iterate, no manual indexing needed.

for (var (k, v) in [(1, 'a')]) {
  print(v);
}

Cleaner, Flatter Code

Patterns replace stacks of .field lookups with one readable line. Your intent becomes obvious at a glance.

Quick Check

What does the underscore do in the pattern var (_, x) = ('a', 1)?

Recap: Destructuring

Well done! 🙌 Patterns let you destructure records, lists, and maps into named variables, with wildcards and rest elements for a clean fit.

Frequently asked questions

Is the “Destructuring With Patterns” lesson free?

Yes — the full text of “Destructuring With Patterns” 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 “Destructuring With Patterns”?

Pull apart records, lists, and maps. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Destructuring With Patterns” 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. Records: Lightweight Tuples
  2. Positional and Named Record Fields
  3. Destructuring With Patterns
  4. switch Patterns and Guards
← Back to Dart Academy