avoid_equals_and_hash_code_on_mutable_classes
@immutable
でマークされていないクラスで、operator ==とhashCodeをオーバーロードすることを避けてください。
このルールは、Dart 2.6から利用可能です。
詳細
#避けるべき @immutable
でマークされていないクラスで、operator ==とhashCodeをオーバーロードすること。
クラスが不変でない場合、operator ==
とhashCode
をオーバーロードすると、コレクションで使用された際に予測不可能で望ましくない動作につながる可能性があります。
悪い例
dart
class B {
String key;
const B(this.key);
@override
operator ==(other) => other is B && other.key == key;
@override
int get hashCode => key.hashCode;
}
良い例
dart
@immutable
class A {
final String key;
const A(this.key);
@override
operator ==(other) => other is A && other.key == key;
@override
int get hashCode => key.hashCode;
}
注:このlintは@immutable
アノテーションの使用をチェックし、クラスがそれ以外で可変でない場合でもトリガーされます。
悪い例
dart
class C {
final String key;
const C(this.key);
@override
operator ==(other) => other is C && other.key == key;
@override
int get hashCode => key.hashCode;
}
使用方法
#avoid_equals_and_hash_code_on_mutable_classes
ルールを有効にするには、analysis_options.yaml
ファイルのlinter > rulesの下にavoid_equals_and_hash_code_on_mutable_classes
を追加します。
analysis_options.yaml
YAML
linter:
rules:
- avoid_equals_and_hash_code_on_mutable_classes
特に明記されていない限り、このサイトのドキュメントはDart 3.5.3を反映しています。ページ最終更新日:2024年7月3日。 ソースを表示 または 問題を報告する。