跳到主要內容

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