Google News
logo
Dart - Interview Questions
What is a double dot in Dart?
In Dart, the double dot (`..`) is known as the cascade notation or cascade operator. It provides a concise way to perform a sequence of operations on the same object without repeating the object reference.

The cascade notation allows you to chain multiple method calls or property assignments on the same object. It is especially useful when you need to perform a series of modifications or operations on an object in a fluent and readable manner.

Here's an example to illustrate the usage of the double dot (`..`) cascade notation:
class Person {
  String name = '';
  int age = 0;
  String address = '';

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

void main() {
  var person = Person()
    ..name = 'John Doe'
    ..age = 30
    ..address = '123 Main St';

  person.printDetails();
}​
In the example above, we define a `Person` class with three properties: `name`, `age`, and `address`. The `printDetails` method prints the values of these properties.

In the `main` function, we create an instance of the `Person` class using the `var` keyword. The double dot (`..`) cascade notation allows us to perform multiple property assignments (`name`, `age`, `address`) on the `person` object without repeating the object reference.

The cascade notation simplifies the code by chaining the property assignments together. It enhances readability and reduces redundancy, especially when multiple modifications need to be made to an object.

When you run the code, it will output :
Name: John Doe
Age: 30
Address: 123 Main St​

The cascade notation (`..`) can be used with any object that has methods or properties. It allows you to chain multiple method calls or property assignments in a concise and readable way, making the code more expressive and compact.
Advertisement