Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Cascade operatore

Saturday, June 8, 2024 8:42 PM

The Dart cascade operator (`..`) is a handy tool for performing multiple actions on the same
object in a row, without having to repeat the object's name each time. Here's a simple
explanation with an example:
Explanation:
Think of the cascade operator (`..`) as a way to talk to an object multiple times in a row
without having to say its name over and over again.
Example:
Suppose you have a `Person` object with `name` and `age` properties, and a `sayHello()`
method to greet the person.
class Person {
String name = '';
int age = 0;

void sayHello() {
print('Hello, my name is $name and I am $age years old.');
}
}
Now, let's create a `Person` object and set its properties and call its method using the
cascade operator:
void main() {
var person = Person() // Create a new Person object
..name = 'Alice' // Set the name property
..age = 30 // Set the age property
..sayHello(); // Call the sayHello() method
}
Summary:
- The cascade operator (`..`) lets you perform multiple actions on the same object in a row,
like setting properties or calling methods.
- It saves you from having to repeat the object's name for each action, making your code
more concise and easier to read.
-That's it! The cascade operator is a simple but powerful feature in Dart for working with
objects more efficiently.

Dart Page 1

You might also like