Skip to main content

use_to_and_as_if_applicable

Stable

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:

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);
  }
}

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:

analysis_options.yaml
yaml
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:

analysis_options.yaml
yaml
linter:
  rules:
    use_to_and_as_if_applicable: true