跳到主要内容

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