Skip to main content

prefer-getter-over-method

added in: 1.9.0
⚙️

Suggests to convert a method that has no parameters and side-effects to a getter.

⚙️ Config

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

dart_code_metrics:
...
rules:
...
- prefer-getter-over-method:
ignored-names:
- methodName

Example

❌ Bad:

extension StringExtension on String {
// LINT
String useCorrectEllipsis() {
return replaceAll('', '\u200B');
}

String removeSpaces() => replaceAll(' ', ''); // LINT

String removeHyphens() => replaceAll('-', ''); // LINT
}

✅ Good:

extension StringExtension on String {
String get useCorrectEllipsis => replaceAll('', '\u200B');

String get removeSpaces => replaceAll(' ', '');

String get removeHyphens => replaceAll('-', '');

String toValue() => this;
}