use_to_and_as_if_applicable
Start the name of the method with to/_to or as/_as if applicable.
This rule is available as of Dart 2.0.
Details
#From Effective Dart:
PREFER naming a method to___()
if it copies the object's state to a new
object.
PREFER naming a method as___()
if it returns a different representation
backed by the original object.
BAD:
dart
class Bar {
Foo myMethod() {
return Foo.from(this);
}
}
GOOD:
dart
class Bar {
Foo toFoo() {
return Foo.from(this);
}
}
GOOD:
dart
class Bar {
Foo asFoo() {
return Foo.from(this);
}
}
Usage
#To enable the use_to_and_as_if_applicable
rule,
add use_to_and_as_if_applicable
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- use_to_and_as_if_applicable
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。