use_to_and_as_if_applicable
Start the name of the method with to/_to or as/_as if applicable.
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:
class Bar {
Foo myMethod() {
return Foo.from(this);
}
}
GOOD:
class Bar {
Foo toFoo() {
return Foo.from(this);
}
}
GOOD:
class Bar {
Foo asFoo() {
return Foo.from(this);
}
}
Enable
#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:
linter:
rules:
- use_to_and_as_if_applicable
If you're instead using the YAML map syntax to configure linter rules, add use_to_and_as_if_applicable: true
under linter > rules:
linter:
rules:
use_to_and_as_if_applicable: true
Unless stated otherwise, the documentation on this site reflects Dart 3.7.0. Page last updated on 2025-01-27. View source or report an issue.