跳到主要內容

use_to_and_as_if_applicable

穩定

如果適用,方法名應以 to/_to 或 as/_as 開頭。

詳情

#

來自 高效 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 檔案中,將 use_to_and_as_if_applicable 新增到 linter > rules 下方

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