no_leading_underscores_for_local_identifiers
避免局部标识符使用前导下划线。
详细信息
#不要对非私有标识符使用前导下划线。Dart 使用标识符中的前导下划线来标记成员和顶级声明为私有。这训练用户将前导下划线与这些类型的声明相关联。他们看到 _
就会认为“私有”。对于局部变量或参数,没有“私有”的概念。当其中一个的名称以下划线开头时,会向读者发送混淆的信号。为避免这种情况,不要在这些名称中使用前导下划线。
例外::未使用的参数可以命名为 _
、__
、___
等。这在回调中是常见的做法,你在回调中接收到一个值但不需要使用它。将其命名为仅由下划线组成的名称是表示该值未被使用的惯用方式。
不推荐 (BAD)
dart
void print(String _name) {
var _size = _name.length;
...
}
推荐 (GOOD)
dart
void print(String name) {
var size = name.length;
...
}
可接受 (OK)
dart
[1,2,3].map((_) => print('Hello'));
启用
#要启用 no_leading_underscores_for_local_identifiers
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 no_leading_underscores_for_local_identifiers
analysis_options.yaml
yaml
linter:
rules:
- no_leading_underscores_for_local_identifiers
如果你改用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 no_leading_underscores_for_local_identifiers: true
analysis_options.yaml
yaml
linter:
rules:
no_leading_underscores_for_local_identifiers: true