Skip to content

top_level_shadows_inherited_member

Reports a name in instance code that means a top-level declaration although the object inherits a member of the same name. Write this.name if you mean the member, or rename one of the two.

Tier
advisory
Scope
Dart
Lowest preset
strict
Fix
none

A name inside instance code that refers to a top-level declaration although the object has an inherited member of that name. The violation covers the name.

An inherited member is not in the lexical scope of a class body, but a declaration of the library or of an import is. So the name means the top-level declaration, and only this.name reaches the member.

  • A class that declares the member itself is never reported.
  • In an extension, the members of the extended type count.
  • Only a name without a target is looked at, in an instance method, getter or setter, a constructor body, or the initializer of a late instance field.
  • A read or a call is matched with an inherited getter or method, a write with an inherited setter.

It does not report:

  • a call with arguments the inherited method cannot take: it means the top-level function on purpose;
  • a private member of another library, which is out of reach;
  • a name in a comment.

The same line means different things in two classes that look alike: declaring the member in the class itself changes what the name refers to. Members of interfaces count too, since an abstract class that implements an interface without declaring its members reaches them through this.

Violating
int size = 1;
class Reader extends Sized {
int read() => size;
}
Corrected
int size = 1;
class Reader extends Sized {
int read() => this.size;
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// The library's size is meant here, not the widget's.
// ignore: assay/top_level_shadows_inherited_member
int read() => size;

// ignore_for_file: assay/top_level_shadows_inherited_member silences it in a whole file, and // ignore: top_level_shadows_inherited_member without the prefix works too. See Suppressions.