Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page

Was this helpful?

  1. Dart Fundamentals-2
  2. Miscellaneous concepts

Cascade notation

PreviousImmutable InstancesNextSection Overview

Last updated 4 years ago

Was this helpful?

void main() {
  var employee1 = Employee();

//   employee1.id = 1;

//   employee1.firstname = "John";

//   employee1.lastname = "Doe";

//   employee1.position = "CTO";

  employee1
    ..id = 1
    ..firstname = "John"
    ..lastname = "Doe"
    ..position = "CTO";

  print(employee1.toString());
}

class Employee {
  int id;

  String firstname;

  String lastname;

  String position;

  @override
  String toString() {
    return "id : $id , firstname : $firstname , lastname : $lastname, position : $position";
  }
}

void main() {
  // Cascade Notation

  var calculator1 = Calculator(3);

//   calculator1.add(12);
//   calculator1.subtract(12);
//   calculator1.add(5);
//   calculator1.subtract(9);

  calculator1
    ..add(12)
    ..subtract(12)
    ..add(5)
    ..subtract(9);

  print(calculator1.result());
}

class Calculator {
  int output = 0;

  Calculator(int startValue) {
    this.output = startValue;
  }

  void add(int val) {
    this.output += val;
  }

  void subtract(int val) {
    this.output -= val;
  }

  int result() {
    return this.output;
  }
}


Using the cascade notation with methods is also known as method chaining . You are consecutively calling methods i.e. chaining methods