目录

unnecessary_getters_setters

Avoid wrapping fields in getters and setters just to be "safe".

此规则自 Dart 2.0 版本起可用。

_规则集:recommended , flutter _

此规则提供 快速修复

详情

#

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:

dart
class Box {
  var _contents;
  get contents => _contents;
  set contents(value) {
    _contents = value;
  }
}

GOOD:

dart
class Box {
  var contents;
}

使用方法

#

要启用 unnecessary_getters_setters 规则,请在你的 analysis_options.yaml 文件中,在 linter > rules 下添加 unnecessary_getters_setters

analysis_options.yaml
yaml
linter:
  rules:
    - unnecessary_getters_setters