Getter returns itself and recurses forever
ID |
dart.correctness.recursive_getters |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Logic |
Language |
Dart |
Description
Reports a getter whose body returns the getter itself, either as an arrow body (int get count ⇒ count;) or as a single return statement, with or without a this. qualifier. Reading the property calls the getter again, without end.
Rationale
The getter never returns a value: each read re-enters it, so the first access overflows the stack and the program dies with a StackOverflowError that names only the getter, giving no hint of what was actually meant. The mistake is almost always a missing underscore - the backing field is _count and the getter body should read that field, not the property. Dart will not warn about it, because a getter calling a getter is legal, and the recursion is only obvious once the two names are compared side by side.
The following code illustrates the pattern detected by this rule:
class Temperature {
double _celsius = 0;
int _count = 0;
// FLAGGED: Getter returns itself and recurses forever
double get celsius => celsius;
Temperature get _inner => this;
}