flutter dart cascade opetator notation
cascade operator in dart language

Estimated reading time: 1 minute

The Cascade operator notation in flutter dart (..) is two dots, using cascade in dart we can make a sequence of operation using same object & we can even call a function & easily access field or parameter value. By using dart cascade notation (..) we can keep dart code neat & clean as it removes unnecessary temporary variable creation to store data.

Video Tutorial on Flutter Cascade Operator

Cascade Operator in dart example code

In below example We have created a class by name Person which has 2 variable and 1 method to print on console:

  • int? age;
  • String? name;
  • printDetails()

class person

class Person{
  int? age;
  String? name;
  
  void printDetails(){
    print("$name age is $age");
  }
}

In Below Code,

Line No: 3 – > Create an object of Person as p1.

void main(){
  
  Person p1 = Person(); // object creation

  p1..name = "Rajat Palankar"..age = 26 ..printDetails(); 
  // using cascade, initialize the value and call print function
  
}

Then, In line No: 5 -> using object, initialize the value to class variable and call printDetails method of Person Class.

Output : Rajat Palankar age is 26