0Pricing
Dart Academy · Lesson

Spread and Null-Aware Spread

Inline one collection into another.

Spread and Null-Aware Spread 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.

Combining Collections

Often you want to drop all items of one list into another. Dart's spread operator inlines a whole collection in a single move.

Meet Three Dots

The spread operator is three dots placed before a collection inside a list, set, or map literal. Its items pour right in.

var more = [4, 5];
var all = [1, 2, 3, ...more];

What Spread Produces

Spread copies the source items into the new collection. The result is a fresh list; your original collection stays untouched.

Spread Anywhere

You can place a spread at the start, middle, or end, and mix several spreads with normal items in the very same literal.

var x = [0, ...a, 9, ...b];

The Null Problem

If the collection you spread is null, a plain spread throws an error. That is risky when the source might not exist yet.

Null-Aware Spread

Add a question mark for the null-aware spread, written as question mark then three dots. A null source simply contributes nothing.

var all = [1, 2, ...?maybeNull];

Skips, Never Crashes

With null-aware spread, a null collection is just skipped. You get the rest of the items and zero runtime error.

Spread Works on Sets

Spread is not list-only. Inside a set literal it pours items in too, and duplicates are dropped to keep the set unique.

var s = {1, 2, ...other};

Spread Works on Maps

You can spread one map into another to merge entries. Later keys win, so a spread can override earlier values neatly.

var m = {'a': 1, ...defaults};

Spread vs addAll

Spread builds a new collection in a literal, while addAll mutates an existing one. Reach for spread when you want a fresh result.

Why It Reads Well

Spread keeps your intent visible: you see exactly which pieces form the new collection, all in one tidy literal. 📦

Quick Check

What happens with a null-aware spread on a null collection?

Recap

The spread (...) inlines a collection into a literal, and the null-aware spread (...?) safely skips a null source. Works on lists, sets, and maps. 🎯

Frequently asked questions

Is the “Spread and Null-Aware Spread” lesson free?

Yes — the full text of “Spread and Null-Aware Spread” 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 “Spread and Null-Aware Spread”?

Inline one collection into another. 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 “Spread and Null-Aware Spread” 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. The Cascade Operator (..)
  2. Null-Shorting Cascades (?..)
  3. Spread and Null-Aware Spread
  4. Collection if and for
← Back to Dart Academy