unnecessary-getters-setters
Same as unnecessary_getters_setters in Dart lints
- Group
- Complexity
- Path
lint/complexity/unnecessary-getters-setters- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 456 of 512
Flags a trivial getter/setter pair that only reads and writes a private field.
When a getter merely returns _x and a setter merely assigns _x with no
validation or side effect, the pair buys nothing over a public field — Dart
lets you later replace a public field with a getter/setter without changing
call sites, so the wrapping is premature. The rule fires only when a getter
and setter share a name, both target the same backing field, and that field
is private (has a leading underscore). Metadata on either accessor exempts
the pair.
Invalid
// Trivial getter/setter pairs that merely expose a private field.
class A {
int _value = 0;
int get value => _value;
set value(int v) => _value = v;
}
class B {
String _name = '';
String get name { return _name; }
set name(String v) { _name = v; }
}
class C {
bool _flag = false;
bool get flag => _flag;
set flag(bool v) { _flag = v; }
}
class D {
double _size = 0;
double get size => this._size;
set size(double v) { this._size = v; }
}
class E {
num _count = 0;
num get count => _count;
set count(num v) => _count = v;
} int _value = 0; int get value => _value; String _name = ''; String get name { return _name; } bool _flag = false; bool get flag => _flag; double _size = 0; double get size => this._size; num _count = 0; num get count => _count;Valid
// Accessors with real logic, lone accessors, or plain public fields.
class A {
int _value = 0;
int get value => _value * 2;
set value(int v) => _value = v;
}
class B {
int _n = 0;
int get n => _n;
}
class C {
int _c = 0;
set c(int v) {
_c = v;
}
}
class D {
int _d = 0;
int get d => _d;
set d(int v) {
_d = v < 0 ? 0 : v;
}
}
class E {
int value = 0;
}
class F {
int _f = 0;
int get f => _f;
set f(int v) {
_f = v;
print(v);
}
}
class AnnotatedGetter {
int _value = 0;
@deprecated
int get value => _value;
set value(int value) => _value = value;
}
class AnnotatedSetter {
int _value = 0;
int get value => _value;
@deprecated
set value(int value) => _value = value;
}Configure unnecessary-getters-setters
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"unnecessary-getters-setters": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/unnecessary-getters-setters: <reason>More complexity rules
- unnecessary-lambdas
unnecessary_lambdasrecommendedFlags a closure that only forwards its parameters to a function a tear-off could name.
- unnecessary-overrides
unnecessary_overridesrecommendedFlags a member that does nothing but forward to
superwith the same arguments and resolved contract. - widget-tree-depthflutter
Limits the depth of an inline Flutter widget construction tree.
- avoid-classes-with-only-static-members
avoid_classes_with_only_static_membersFlags a class that declares only static members.
- avoid-collapsible-if
Flags an else-free
ifwhose only then-statement is another else-freeif. - avoid-complex-arithmetic-expressions
Reports arithmetic expressions containing too many terms.
- avoid-complex-conditions
Flags boolean expressions with more logical operands than the configured threshold.
- avoid-complex-loop-conditions
Flags loop conditions with more logical operands than the configured threshold.