prefer-static-method
Suggests converting an instance method to a static method if it does not reference any instance or inherited members.
⚙️ Config
Set ignore-public
(default is false
) to ignore public methods.
Set ignore-private
(default is false
) to ignore private methods.
analysis_options.yaml
dart_code_metrics:
rules:
- prefer-static-method:
ignore-public: false
ignore-private: false
note
Only one of the configuration options will apply if both are set to true
.
Example
❌ Bad:
class Child extends Parent {
const Child() : super('hello');
// LINT: This method does not reference any instance members and can be made static.
void some() {
print('hi');
}
}
✅ Good:
class Child extends Parent {
const Child() : super('hello');
void correct() {
super.value;
super.method();
this.value;
}
// OR
static void another() {
...
}
}
// OR
void globalFn() {
...
}