avoid-unnecessary-getter
Warns when a getter provides access to an existing final field without any additional logic.
⚙️ Config
Set ignore-upcast
(default is true
) to ignore getters with the different return type than the referenced field (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-unnecessary-getter:
ignore-upcast: true
Example
❌ Bad:
abstract class PageRoute<T> extends ModalRoute<T> {
PageRoute();
bool get barrierDismissible => _barrierDismissible; // LINT: Unnecessary getter. Try replacing it with an existing field declaration and making it public.
final _barrierDismissible = false;
}
✅ Good:
abstract class PageRoute<T> extends ModalRoute<T> {
PageRoute();
final barrierDismissible = false; // Correct, public immutable field instead
String? _contents;
String? get contents => _contents;
set contents(String? value) {
_contents = value;
}
}
Example with "ignore-upcast"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-unnecessary-getter:
ignore-upcast: false
❌ Bad:
class SomeClass {
final List<String>? _list;
Iterable<String> get list => _list; // LINT: Unnecessary getter. Try replacing it with an existing field declaration and making it public.
}