Contents

use_to_and_as_if_applicable

Contents

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