prefer-getter-over-method
Suggests to convert a method that has no parameters and side-effects to a getter.
note
You can fix this rule's issues using the "Convert Method to Getter" assist.
⚙️ Config
Set ignored-names
(default is empty) to ignore specific names.
Set ignore-await
(default is false
) to ignore declarations that use await
.
analysis_options.yaml
dart_code_metrics:
rules:
- prefer-getter-over-method:
ignored-names:
- methodName
ignore-await: false
Example
❌ Bad:
extension StringExtension on String {
// LINT: This method declaration can be converted to a getter.
String useCorrectEllipsis() {
return replaceAll('', '\u200B');
}
String removeSpaces() => replaceAll(' ', ''); // LINT: This method declaration can be converted to a getter.
String removeHyphens() => replaceAll('-', ''); // LINT: This method declaration can be converted to a getter.
}
✅ Good:
extension StringExtension on String {
String get useCorrectEllipsis => replaceAll('', '\u200B');
String get removeSpaces => replaceAll(' ', '');
String get removeHyphens => replaceAll('-', '');
String toValue() => this;
}