use_to_and_as_if_applicable
如果适用,方法名以 to/_to 或 as/_as 开头。
详情
#推荐 将复制对象状态到新对象的方法命名为 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
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。