Skip to main content

avoid-unnecessary-getter

configurable
pro+

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();

// LINT: Unnecessary getter.
// Try replacing it with an existing field declaration and making it public.

bool get barrierDismissible => _barrierDismissible;
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;
// LINT: Unnecessary getter.
// Try replacing it with an existing field declaration and making it public.
Iterable<String> get list => _list;
}