prefer-getter-over-method

Group
Style
Path
lint/style/prefer-getter-over-method
Default
Off, opt in
Origin
Falcon
Rule
365 of 512

Prefer getters for synchronous, zero-argument methods that only read a field.

Only unannotated instance methods on classes without inheritance are checked. The result must be a directly declared, non-late instance field, not another getter or an arbitrary expression. Generic methods and overridden contracts are excluded. Both arrow bodies and single-return blocks are supported.

Invalid

example.dartdart
class Box {
  final int _size = 3;
  int size() => _size;
  int otherSize() { return this._size; }
}
This zero-argument method only reads a field; expose it as a getter instead.
2 final int _size = 3;
3 int size() => _size;
This zero-argument method only reads a field; expose it as a getter instead.
3 int size() => _size;
4 int otherSize() { return this._size; }

Valid

example.dartdart
class Box {
  int _size = 3;
  late int _lazy;
  external int externalValue;
  int readExternal() => externalValue;
  final String _label = 'box';
  String toString() => _label;
  int call() => _size;
  final Map<String, Object?> _json = {};
  Map<String, Object?> toJson() => _json;
  int get size => _size;
  int compute() => _size + 1;
  int update() => ++_size;
  int lazy() => _lazy;
  int withArgument(int value) => _size;
  T generic<T>() => throw StateError('missing');
  Future<int> asyncSize() async => _size;
  static int staticSize() => 3;
  int get dynamicValue => _size;
  int fromGetter() => dynamicValue;
  int sideEffect() { _size++; return _size; }
}
class Child extends Box {
  int inherited() => _size;
}

Configure prefer-getter-over-method

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-getter-over-method": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-getter-over-method: <reason>
  • prefer-initializing-formalsprefer_initializing_formalsrecommended

    Prefer initializing formals when a parameter is copied directly into its field.

  • prefer-inlined-addsprefer_inlined_addsrecommended

    Flags an add cascade on a list literal whose element could be written inline.

  • prefer-int-literalsprefer_int_literals

    Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.

  • prefer-is-not-emptyprefer_is_not_emptyrecommended

    Flags .length comparisons equivalent to a .isNotEmpty check.

  • prefer-is-not-operatorprefer_is_not_operatorrecommended

    Flags a negated type test !(x is T) that should use the is! operator.

  • prefer-iterable-ofprefer-iterable-ofrecommended

    Flags .from collection constructors in favor of the .of constructor.