unnecessary-getters-setters

Same as unnecessary_getters_setters in Dart lints

Path
lint/complexity/unnecessary-getters-setters
Default
On (recommended)
Origin
Dart lints
Rule
456 of 512
recommended

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

example.dartdart
// 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;
}
Avoid wrapping a field in trivial getters and setters; use a public field.
3 int _value = 0;
4 int get value => _value;
Avoid wrapping a field in trivial getters and setters; use a public field.
9 String _name = '';
10 String get name { return _name; }
Avoid wrapping a field in trivial getters and setters; use a public field.
15 bool _flag = false;
16 bool get flag => _flag;
Avoid wrapping a field in trivial getters and setters; use a public field.
21 double _size = 0;
22 double get size => this._size;
Avoid wrapping a field in trivial getters and setters; use a public field.
27 num _count = 0;
28 num get count => _count;

Valid

example.dartdart
// 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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "unnecessary-getters-setters": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/unnecessary-getters-setters: <reason>