跳到主要內容

use_to_and_as_if_applicable

穩定

如果適用,方法名稱開頭請使用 to/\_to 或 as/\_as。

詳細資訊

#

出自 Effective Dart

建議 如果方法將物件的狀態複製到新物件,則將方法命名為 to___()

建議 如果方法傳回由原始物件支援的不同表示法,則將方法命名為 as___()

不良範例

dart
class Bar {
  Foo myMethod() {
    return Foo.from(this);
  }
}

良好範例

dart
class Bar {
  Foo toFoo() {
    return Foo.from(this);
  }
}

良好範例

dart
class Bar {
  Foo asFoo() {
    return Foo.from(this);
  }
}

啟用

#

若要啟用 use_to_and_as_if_applicable 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 use_to_and_as_if_applicable

analysis_options.yaml
yaml
linter:
  rules:
    - use_to_and_as_if_applicable

如果您改為使用 YAML 地圖語法來設定 linter 規則,請在 linter > rules 下新增 use_to_and_as_if_applicable: true

analysis_options.yaml
yaml
linter:
  rules:
    use_to_and_as_if_applicable: true