目录

use_to_and_as_if_applicable

Start the name of the method with to/_to or as/_as if applicable.

此规则自 Dart 2.0 版本起可用。

详情

#

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

使用方法

#

要启用 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