0Pricing
Dart Academy · Lesson

Collection if and for

Build lists conditionally and in loops.

Logic Inside Literals

Sometimes a list's contents depend on a condition. Dart lets you put an if right inside the collection literal, so you build it in one place.

Collection if

A collection if adds an element only when its condition is true. No condition met means that slot simply contributes nothing.

var nav = ['Home', if (loggedIn) 'Profile'];

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