跳到主要內容

constant_identifier_names

穩定
推薦
可修復

常量名稱優先使用 lowerCamelCase。

詳情

#

優先使用 lowerCamelCase 作為常量名稱。

在新程式碼中,常量變數(包括列舉值)使用 lowerCamelCase

在使用 ALL_CAPS_WITH_UNDERSCORES 作為常量的現有程式碼中,您可以繼續使用全大寫以保持一致性。

錯誤示例 (BAD)

dart
const PI = 3.14;
const kDefaultTimeout = 1000;
final URL_SCHEME = RegExp('^([a-z]+):');

class Dice {
  static final NUMBER_GENERATOR = Random();
}

正確示例 (GOOD)

dart
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');

class Dice {
  static final numberGenerator = Random();
}

啟用

#

要啟用 constant_identifier_names 規則,請在您的 analysis_options.yaml 檔案中,將 constant_identifier_names 新增到 linter > rules

analysis_options.yaml
yaml
linter:
  rules:
    - constant_identifier_names

如果您使用 YAML map 語法配置 linter 規則,請在 linter > rules 下新增 constant_identifier_names: true

analysis_options.yaml
yaml
linter:
  rules:
    constant_identifier_names: true