On this page12 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. The Problem
  4. 2. The Cascade
  5. 3. Cascades on Lists and Other Objects
  6. 4. The Key Rule: You Get the Object Back
  7. 5. The Null-Aware Cascade ?..
  8. Code Examples
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 029: Task: Cascade Notation

Day10: Functions & Parameters
Lecture029 of 057
Estimated video length10 min
TargetDart 3.12
LevelBeginner

Setting up an object often means writing its name over and over: p.name = ..., p.age = ..., p.city = .... The cascade operator .. lets you do many things to the same object in one flowing statement. Flutter code uses it a lot.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lectures 011 (lists) and 025 (functions and methods). The examples use a tiny class; classes are explained properly on Day 11, so for now just read class Person as "a type with some fields".


Learning Objectives

  • Chain several operations on one object with ..
  • Set fields and call methods in a cascade
  • Know that a cascade gives back the object, not the method's result
  • Use the null-aware cascade ?..

1. The Problem

dart
class Person {
  String name = '';
  int age = 0;
  String city = '';

  void introduce() {
    print('I am $name, $age, from $city');
  }
}

var p1 = Person();
p1.name = 'Ali';
p1.age = 25;
p1.city = 'Lahore';
p1.introduce();   // I am Ali, 25, from Lahore

p1. is repeated four times.

2. The Cascade

.. means "and then, on the same object…":

dart
var p2 = Person()
  ..name = 'Sara'
  ..age = 22
  ..city = 'Karachi'
  ..introduce();

print(p2.name);   // Sara

Output

code
I am Sara, 22, from Karachi
Sara

Everything after Person() is applied to that one new object, and p2 gets the object itself.

3. Cascades on Lists and Other Objects

dart
var list = <int>[]
  ..add(3)
  ..add(1)
  ..add(2)
  ..sort();
print(list);   // [1, 2, 3]

var buffer = StringBuffer()
  ..write('Hello')
  ..write(', ')
  ..write('World');
print(buffer.toString());   // Hello, World

var sorted = [5, 1, 4]..sort();
print(sorted);   // [1, 4, 5]

4. The Key Rule: You Get the Object Back

list.sort() returns nothing (void). But list..sort() returns the list:

ExpressionResult
[5, 1, 4].sort()nothing (void)
[5, 1, 4]..sort()the sorted list

That's why var sorted = [5, 1, 4]..sort(); works and var sorted = [5, 1, 4].sort(); doesn't (see Mistakes).

5. The Null-Aware Cascade ?..

If the object might be null, start with ?... The whole cascade is skipped when it's null:

dart
List<int>? maybe;
maybe?..add(1)..add(2);
print(maybe);   // null: nothing ran, no crash

Code Examples

Example 1: Building an Order

dart
class Order {
  String customer = '';
  final List<String> items = [];
  double discount = 0;

  void addItem(String item) => items.add(item);

  void summary() {
    print('Order for $customer: ${items.join(', ')} (discount ${(discount * 100).round()}%)');
  }
}

void main() {
  Order()
    ..customer = 'Bilal'
    ..addItem('Burger')
    ..addItem('Fries')
    ..addItem('Cola')
    ..discount = 0.1
    ..summary();
}

Output

code
Order for Bilal: Burger, Fries, Cola (discount 10%)

One statement creates the order, fills it in and prints it, without needing a variable at all.


Common Beginner Mistakes

1. Using . where you meant ..

dart
var list = [3, 1, 2].sort();
print(list);
code
Error: This expression has type 'void' and can't be used.

.sort() gives back nothing. Write [3, 1, 2]..sort().

2. Starting a cascade on a void result

dart
var list = [3, 1, 2].sort()..add(4);
code
Error: This expression has type 'void' and can't be used.
Error: The method 'add' isn't defined for the type 'void'.

The cascade must start on the object: [3, 1, 2]..sort()..add(4).

3. A ; in the middle: each .. line is part of one statement. Only the last line ends with ;.


Practice Questions

  1. What does .. do?
  2. What is x after var x = [3, 2, 1]..sort();?
  3. Rewrite with a cascade: var b = StringBuffer(); b.write('A'); b.write('B');
  4. Why does var x = [3, 2, 1].sort(); fail to compile when you use x?
  5. What happens with list?..add(1) when list is null?

Practice Answers

  1. It performs an operation on the same object and gives back that object, so you can chain more operations.
  2. [1, 2, 3]
  3. var b = StringBuffer()..write('A')..write('B');
  4. sort() returns void, so x would hold nothing usable.
  5. Nothing. The cascade is skipped, with no crash.

Key Takeaways

  • object..a()..b = 1..c() does several things to the same object.
  • A cascade gives back the object, not the method's result.
  • [...]..sort() gives the sorted list; [...].sort() gives nothing.
  • ?.. skips the cascade when the object is null.
  • One cascade = one statement; only the end gets ;.