目次

use_to_and_as_if_applicable

メソッドの名前は、該当する場合は to/_to または as/_as で始めます。

このルールは Dart 2.0 から利用可能です。

詳細

#

Effective Dart より

オブジェクトの状態を新しいオブジェクトにコピーする場合は、メソッドに to___() という名前を付けることを推奨します

元のオブジェクトによって裏付けられた異なる表現を返す場合は、メソッドに as___() という名前を付けることを推奨します

悪い例

dart
class Bar {
  Foo myMethod() {
    return Foo.from(this);
  }
}

良い例

dart
class Bar {
  Foo toFoo() {
    return Foo.from(this);
  }
}

良い例

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