Dart Operators : Cascade Notation

Dart Operators : Cascade Notation

·

1 min read

Cascade Notation

  • The cascade notation (. .) in Dart allows us to make a sequence of operations on the same object (including function calls and field access). This notation helps keep Dart code compact and removes the need to create temporary variables to store data.
class Person {
  late String name;
  late int age;

  void setDetails(String newName, int newAge) {
    name = newName;
    age = newAge;
  }

  void printDetails() {
    print('Name: $name, Age: $age');
  }
}

void main() {
  var person = Person();

  // Without cascade notation
  person.setDetails('Jeet', 19);
  person.printDetails();

  // With cascade notation
  person
    ..setDetails('Sahil', 20)
    ..printDetails();

  person
    ..setDetails('newName', 21)
    ..printDetails();
}
Name: Jeet, Age: 19
Name: Sahil, Age: 20
Name: newName, Age: 21

Exited.