Skip to main content

avoid-duplicate-cascades

added in: 1.11.0
⚙️🛠
Pro+
preset: recommended

Warns when a cascade expression has duplicate cascades.

Duplicate cascades are usually the result of a typo and a sign of a bug.

⚙️ Config

Set ignored-types (default is empty) to allow duplicate cascades on the variables of the configured type (example).

analysis_options.yaml
dart_code_metrics:
rules:
- avoid-duplicate-cascades:
ignored-types:
- BuildContext

Example

❌ Bad:

void fn() {
final value = SomeClass();

value
..field = '2' // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..field = '1' // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..field = '1' // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..another = '1';

value
..method(() => 1) // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..method(() => 1) // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..method(() => 2);

final list = [1, 2, 3];

list
..[1] = 2 // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..[1] = 2 // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..[1] = 3 // LINT: Avoid duplicate cascades. Try removing the duplicate cascade expression or replacing it with a different expression.
..[2] = 2;
}

class SomeClass {
String? field;
String? another;

void method(Function callback) {}
}

✅ Good:

void fn() {
final value = SomeClass();

value
..field = '2'
..another = '1'; // Correct, different target fields

value
..method(() => 1)
..method(() => 2); // Correct, different callbacks

final list = [1, 2, 3];

list
..[1] = 2;
..[2] = 3; // Correct, different target indexes
}

class SomeClass {
String? field;
String? another;

void method(Function callback) {}
}

Example with "ignored-types"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-duplicate-cascades:
ignored-types:
- BuildContext

✅ Good:

void someFunction(BuildContext context) {
// Correct, BuildContext is ignored
context
..pop()
..pop();
}

Additional Resources