avoid-referencing-subclasses
Warns when a parent class references its child class.
Subclass references can be confusing and violate several rules of object-oriented design. Consider moving such references out of the parent class.
⚙️ Config
Set ignored-annotations
(default is [freezed
]) to ignore declarations with specific annotations.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-referencing-subclasses:
ignored-annotations:
- someAnnotation
Example
❌ Bad:
class Parent {
// LINT: Avoid referencing subclasses in their parent classes.
void doWork(Child sub) {}
// LINT: Avoid referencing subclasses in their parent classes.
Child getChild(String value) {}
// LINT: Avoid referencing subclasses in their parent classes.
GrandChild getGrandChild(String value) {}
}
class Child extends Parent {}
class GrandChild extends Child {}
✅ Good:
class Parent {}
class Child extends Parent {}
class GrandChild extends Child {}