-3.1 C
New York
Wednesday, November 29, 2023

Using Cascade Notation in Dart and Flutter

This short post walks you through a few examples of using the cascade notation (.., ?…) in Dart.

What Is The Point?

In Dart, cascades let you execute a sequence of operations on the same object. In other words, cascades allow you to perform a chain of methods without the need for temporary variables. In general, they will look like this:Advertisements


For more clarity, see the examples below.

Example 1: Cascades and a List

The two code snippets below do the same thing.

  1. Using cascades:
void main() {
  List myList = [1, 2, 3];


// Output: [4, 5, 6]

2. Not using cascades:

void main() {
  List myList = [1, 2, 3];

// Output: [4, 5, 6]

Example 2: Cascades and a Class

AdvertisementsThe code:

class Person {
  String? name;
  int? age;

  void setName(String name) {
    this.name = name;

  void setAge(int age) {
    this.age = age;

  void printInfo() {
    print("User name: ${this.name ?? 'Unknown'}");
    print("User age: ${this.age ?? 'Not provided'}");

void main() {
  new Person()

  final instance = new Person();
    ..setName('John Doe')


// Person 1
User name: Kindacode.com
User age: 4

// Person 2
User name: John Doe
User age: 99


We’ve gone through a couple of examples of using cascades in Dart. At this point, you should have a better understanding and get a comfortable sense of reading and writing concise and compact code with this syntax. If you’d like to explore more new and interesting features in Dart and Flutter, take a look at the following posts:


You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.


Related Articles

Latest Articles