avoid-unnecessary-getter

Group
Style
Path
lint/style/avoid-unnecessary-getter
Default
Off, opt in
Origin
Falcon
Rule
163 of 512

Flags a getter that only exposes a private final field.

Replace the backing field and getter with a public final field. Getter/setter pairs are covered separately by unnecessary-getters-setters. By default, getters that upcast the field are retained; set ignore-upcast to false to diagnose those too.

Invalid

example.dartdart
class Arrow {
  final int _value = 1;
  int get  value => _value;
}
class Body {
  final int _other = 1;
  int get  other { return this._other; }
}
class Annotated {
  final int _hashCode = 1;
  @override
  int get  hashCode => _hashCode;
}
This getter only exposes a final field; replace both with a public final field.
2 final int _value = 1;
3 int get value => _value;
This getter only exposes a final field; replace both with a public final field.
6 final int _other = 1;
7 int get other { return this._other; }
This getter only exposes a final field; replace both with a public final field.
11 @override
12 int get hashCode => _hashCode;

Valid

example.dartdart
class Example {
  final int value = 1;
  int _mutable = 1;
  int get mutable => _mutable;
  int get computed => value + 1;
}
class Pair {
  int _value = 1;
  int get value => _value;
  set value(int value) { _value = value; }
}
class Annotated {
  final int _value = 1;
  @override
  int get hashCode => _value + 1;
}
class Upcast {
  final int _value = 1;
  num get value => _value;
}
class Private {
  final int _value = 1;
  int get _alias => _value;
}
class Late {
  late final int _value;
  int get value => _value;
}

Configure avoid-unnecessary-getter

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-unnecessary-getter": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-unnecessary-getter: <reason>