Dart Operators : Cascade Notation

Dart Operators : Cascade Notation

ยท

2 min read

In simple terms Cascade Notation is use for alternation of object , As we know that we call class in function every time we have to call object with it and code goes to lengthier , apart form that we also write to print statement for the print the value.

While in Cascade notation we simply use " Double Dots " ( .. ) for perform cascade notation , with help that we can call object without calling the object.

Using Cascade Notation we can make shorter our code more efficiently in code.

Here , I have use both method to print value.

void main() {


    //Using object to call
    var student1 = Student();         // One Object, student1 is reference variable
    student1?.id = 01;
    student1?.name = "Vinit";
    student1?.Alldetails();
    student1?.study();
    student1?.sleep();

  //Using Cascade Notation
    var student2 = Student();        // One Object, student2 is reference variable
    student2?..id = 02..name = "Vinit"..Alldetails()..study()..sleep();

}

// Define states (properties) and behavior of a Student
class Student {
    int? id ;             // Instance or Field Variable, default value is null
    String? name;          // Instance or Field Variable, default value is null

    void study() {
        print("${this.name} is now studying");
    }

    void sleep() {
        print("${this.name} is now sleeping");
    }

   void Alldetails()
  {
    print("$id is $name");
  }
}

Here, You can see that we have use ? before dot ( . ) which means if our value is null that it can also be accept , I have to write this sign every time when I call object but if I use Cascade Notation method than I have to write only one time , and It applicable for all the value.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย