Skip to main content

avoid_setters_without_getters

Stable

Avoid setters without getters.

Details

#

DON'T define a setter without a corresponding getter.

Defining a setter without defining a corresponding getter can lead to logical inconsistencies. Doing this could allow you to set a property to some value, but then upon observing the property's value, it could easily be different.

BAD:

dart
class Bad {  int l, r;   set length(int newLength) {  r = l + newLength;  } } 

GOOD:

dart
class Good {  int l, r;   int get length => r - l;   set length(int newLength) {  r = l + newLength;  } } 

Enable

#

To enable the avoid_setters_without_getters rule, add avoid_setters_without_getters under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:  rules:  - avoid_setters_without_getters 

If you're instead using the YAML map syntax to configure linter rules, add avoid_setters_without_getters: true under linter > rules:

analysis_options.yaml
yaml
linter:  rules:  avoid_setters_without_getters: true