2022/10/8/Flutter/Equality in Dart/related sample code

 

訳は文字起こしにあります。


Part1

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Equality in Dart | Decoding Flutter
//パート1:

void main() {
  final a1 = A(title: 'a');
  final a2 = A(title: 'a');
  //デフォルトだと同一インスタンス以外は「等しくない」と評価される。
  //By default, instances other than the same instance are evaluated as "not equal".
  print(a1 == a2); //false
}

class A {
  A({
    required this.title,
  });
  final String title;
}

 


Part2

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Part2:まず自分で==operatorを書き直して、a1とa2を等しいとみなす
//==演算子を定義する。(手動)
//First, I override the == operator and define an == operator that equates a1 and a2. (manual)

void main() {
  final a1 = A(title: 'a');
  final a2 = A(title: 'a');
  //↓==operatorを書き直したのでtrue(等しい)と評価される。
  //↓Evaluates to true (equal) because == operator is overridden.
  print(a1 == a2);// true
}

class A {
  A({
    required this.title,
  });
  final String title;

  @override
  bool operator ==(Object other) {
    return identical(this, other) ||
        (other is A &&
            runtimeType == other.runtimeType &&
            title == other.title);
  }
}

 


Part3

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Part3:hashcodeの定義を書き直して、それを==operatorの定義で使うパターン。(手動)
//A pattern that overrides the hashcode definition and uses it in the ==operator definition.(manual)

//==operatorがhashCodeを使う、hashCodeがtitleを使う、という図式。
//== operator uses hashCode, hashCode uses title.

//a1とa2を等しいとみなす==演算子を定義する。(手動)
//Define a == operator that equates a1 and a2. (manual)

void main() {
  final a1 = A(title: 'a');
  final a2 = A(title: 'a');
  //↓==operatorを書き直したのでtrue(等しい)と評価される。
  //↓Evaluates to true (equal) because == operator is overridden.
  print(a1 == a2); //true
}

class A {
  A({
    required this.title,
  });
  final String title;

  @override
  int get hashCode => title.hashCode;
  //↓フィールドが複数の場合こんな感じ。
  //↓ If there are multiple fields, write it like this.
  //int get hashCode => title.hashCode ^ subtitle.hashCode ^ price.hashCode;

  @override
  bool operator ==(Object other) {
    return identical(this, other) ||
        (other is A &&
            runtimeType == other.runtimeType &&
            hashCode == other.hashCode); //titleの代わりにhashCodeのみで比較できる。
  }
}

 


Part4

Dart Data Class Generatorの詳しい情報は下記を参照ください。

https://marketplace.visualstudio.com/items?itemName=dotup.dart-data-class-generator

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Part4:VSCode拡張機能のDart Data class generatorを使う。(自動)
//A pattern using the Dart Data class generator from the VSCode extension. (automatic)

void main() {
  final a1 = A(title: 'a');
  final a2 = A(title: 'a');
  print(a1 == a2); //true
}

class A {
  A({
    required this.title,
  });
  final String title;

  @override
  bool operator ==(covariant A other) {
    if (identical(this, other)) return true;

    return other.title == title;
  }

  @override
  int get hashCode => title.hashCode;
}

 

 

 


Part5

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Equality in Dart | Decoding Flutter
//Part5:Equatableパッケージを使う(自動)
//Pattern using the Equatable package (automatic)

//a1とa2のhashCodeが同じ。自動的にそういう実装をしてくれるらしい。
// コード自動生成は不要。詳しい情報は下記参照。
//The hashCode of a1 and a2 are the same.
// No code auto-generation required. It seems to do that automatically. See below for more information.
//https://pub.dev/packages/equatable

import 'package:equatable/equatable.dart';

void main() {
  final a1 = A(title: 'a');
  final a2 = A(title: 'a');

  print(a1 == a2); // true
  print(a1.hashCode); //429732014
  print(a2.hashCode); //429732014
}

class A extends Equatable {
  A({
    required this.title,
  });
  final String title;

  @override
  // TODO: implement props
  List<Object?> get props => [title];
}

 


Part6

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Equality in Dart | Decoding Flutter
//Part6:Equatableパッケージを使う(自動)
//Pattern using the Equatable package (automatic)

//AがBを保持している場合。
//If A class holds B class as a field.

//AだけでなくBもEquatableを継承してpropsをオーバーライドする必要がある。
//それをすればa1 == a2がtrueになる。
//Not only A but also B must extend the Equatable class and override the props.
// Then a1 == a2 becomes true.

import 'package:equatable/equatable.dart';

void main() {
  final a1 = A(title: 'a', b: B(name: 'b1'));
  final a2 = A(title: 'a', b: B(name: 'b1'));

  print(a1 == a2);//true
  print(a1.hashCode);
  print(a2.hashCode);
}

class A extends Equatable {
  A({
    required this.title,
    required this.b,
  });
  final String title;
  final B b;

  @override
  List<Object?> get props => [title, b];
}

class B extends Equatable {
  B({required this.name});
  final String name;

  @override
  List<Object?> get props => [name];
}

 


Part7

//https://www.youtube.com/watch?v=DCKaFaU4jdk&t=30s
//Equality in Dart | Decoding Flutter
//Part7:VSCode拡張機能のDart Data class generatorを使う。(自動)
//A pattern using the Dart Data class generator from the VSCode extension. (automatic)

//AがBを保持している場合。
//If A class holds B class as a field.

//こちらもB→Aの順に'generate equatable'をやったら
//a1 == a2がtrueになる。
//Again you need to 'generate equable' for both A and B.
// Then a1 == a2 becomes true.

void main() {
  final a1 = A(title: 'a', b: B(name: 'b'));
  final a2 = A(title: 'a', b: B(name: 'b'));
  print(a1 == a2); // true
}

class A {
  A({
    required this.title,
    required this.b,
  });
  final String title;
  final B b;

  @override
  bool operator ==(covariant A other) {
    if (identical(this, other)) return true;

    return other.title == title && other.b == b;
  }

  @override
  int get hashCode => title.hashCode ^ b.hashCode;
}

class B {
  B({required this.name});
  final String name;

  @override
  bool operator ==(covariant B other) {
    if (identical(this, other)) return true;

    return other.name == name;
  }

  @override
  int get hashCode => name.hashCode;
}

 

 

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です