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
class Box {
final int _size = 3;
int size() => _size;
int otherSize() { return this._size; }
} final int _size = 3; int size() => _size; int size() => _size; int otherSize() { return this._size; }Valid
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.
{
"linter": {
"rules": {
"style": {
"prefer-getter-over-method": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-getter-over-method: <reason>More style rules
- prefer-initializing-formals
prefer_initializing_formalsrecommendedPrefer initializing formals when a parameter is copied directly into its field.
- prefer-inlined-adds
prefer_inlined_addsrecommendedFlags an
addcascade on a list literal whose element could be written inline. - prefer-int-literals
prefer_int_literalsFlags a whole-number double literal, such as
1.0, where the context type isdoubleso the int literal1means the same value. - prefer-interpolation-to-compose-strings
prefer_interpolation_to_compose_stringsrecommendedPrefers string interpolation to composing strings with
+. - prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck. - prefer-is-not-empty
prefer_is_not_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isNotEmptycheck. - prefer-is-not-operator
prefer_is_not_operatorrecommendedFlags a negated type test
!(x is T)that should use theis!operator. - prefer-iterable-of
prefer-iterable-ofrecommendedFlags
.fromcollection constructors in favor of the.ofconstructor.