avoid-suspicious-global-reference
Warns when a subclass or an extension refers to a global identifier, but the parent class has an identifier with the same name.
Usually, in such cases, it is the parent class identifier that is supposed to be referenced, but because of how Dart's resolution works, the global one is used instead. To fix such cases, add explicit this.
or super.
prefixes.
Example
❌ Bad:
const value = 'hi';
class A {
final value = 0;
void work() {
value;
}
}
class C extends A {
void another() {
// LINT: This identifier refers to a global declaration, but the parent class has a field or method with the same name.
// Consider checking for the missing 'this' or 'super'.
value;
}
}
class D {
String get override => 'A.override';
String debug() => 'Debug: $override';
}
extension on D {
// LINT: This identifier refers to a global declaration, but the parent class has a field or method with the same name.
// Consider checking for the missing 'this' or 'super'.
String extDebug() => 'Debug: $override';
}
✅ Good:
const value = 'hi';
class A {
final value = 0;
void work() {
value;
}
}
class C extends A {
void another() {
super.value; // Correct, uses `super`
}
}
class D {
String get override => 'A.override';
String debug() => 'Debug: $override';
}
extension on D {
String extDebug() => 'Debug: ${this.override}'; // Correct, uses `this`
}