avoid_setters_without_getters
Avoid setters without getters.
此规则自 Dart 2.0 版本起可用。
详情
#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;
}
}
使用方法
#要启用 avoid_setters_without_getters
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 avoid_setters_without_getters
:
analysis_options.yaml
yaml
linter:
rules:
- avoid_setters_without_getters
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.