unnecessary_getters_setters
Avoid wrapping fields in getters and setters just to be "safe".
This rule is available as of Dart 2.0.
Rule sets: recommended, flutter
This rule has a quick fix available.
Details
#From Effective Dart:
AVOID wrapping fields in getters and setters just to be "safe".
In Java and C#, it's common to hide all fields behind getters and setters (or properties in C#), even if the implementation just forwards to the field. That way, if you ever need to do more work in those members, you can do it without needing to touch the callsites. This is because calling a getter method is different than accessing a field in Java, and accessing a property isn't binary-compatible with accessing a raw field in C#.
Dart doesn't have this limitation. Fields and getters/setters are completely indistinguishable. You can expose a field in a class and later wrap it in a getter and setter without having to touch any code that uses that field.
BAD:
class Box {
var _contents;
get contents => _contents;
set contents(value) {
_contents = value;
}
}
GOOD:
class Box {
var contents;
}
Usage
#To enable the unnecessary_getters_setters
rule,
add unnecessary_getters_setters
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- unnecessary_getters_setters
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。