Skip to main content

avoid-shadowing

added in: 1.2.0
⚙️
preset: recommended

Warns when a declaration name shadows another declaration with the same name in the current scope.

Shadowing may lead to unexpected errors (especially if the types of both declarations match).

⚙️ Config

Set ignore-static (default is false) to exclude static fields from the name scope.

Set ignore-fields (default is false) to exclude fields from the name scope.

Set ignore-parameters (default is false) to exclude parameters from the name scope.

Set ignored-names (default is none) to ignore specific names.

dart_code_metrics:
...
rules:
...
- avoid-shadowing:
ignore-static: true
ignore-fields: true
ignore-parameters: true
ignored-names:
- some
- name

Example

❌ Bad:

class SomeClass {
final String content;

...

void method() {
final content = ...; // LINT
}

void anotherMethod(String content) { // LINT
...
}

void records() {
final declarations = <(int, String)>{};

var length = 0;

// LINT
if (declarations case Set(:final length)) {
...
}

final Set(length: final length) = declarations; // LINT
}
}

✅ Good:

class SomeClass {
final String content;

...

void method() {
final localContent = ...;
}

void anotherMethod(String contentParam) {
...
}

void records() {
final declarations = <(int, String)>{};

var length = 0;

if (declarations case Set(length: final someLength)) {
...
}

final Set(length: final someLength) = declarations;
}
}