Equatable 的 child class 传递给 super(Equatable class) 的是什么?

what does the child class of Equatable pass to the super(Equatable class)?

嗨,我是 bloc in flutter 的新手,我正在尝试了解块计时器 In the doc of flutter_bloc 我想知道这个构造函数是什么 class 是什么意思


@immutable
abstract class TimerState extends Equatable {
  final int duration;

//and this list props ??
  TimerState(this.duration, [List props = const []])
      : super([duration]..addAll(props));
}

本教程使用equatable: ^0.2.0,在这个版本中当你想覆盖hashcode==运算符时你需要传递一个List的属性给super 构造函数。查看 docs.

考虑到这一点,他创建了一个名为 props 的可选参数,并将包含 duration 和 [=17= 的所有元素的 List 传递给超级构造函数]参数。

abstract class TimerState extends Equatable {
  final int duration;

  TimerState(this.duration, [List props = const []])
      : super([duration]..addAll(props));
}

这样扩展TimerState的class可以使用props可选参数传递其他属性,并且这个属性将被添加到List传递给 TimerStatesuper 构造函数以便正确使用 Equatable

因此,如果您需要一个具有其他属性的状态,您需要这样做:

class OtherTimerState extends TimerState {
  final int otherProperty1;
  final int otherProperty2;

  OtherTimerState(int duration, this.otherProperty1, this.otherProperty2)
      : super(duration, [otherProperty1, otherProperty2]);
}

更新:对于新版本的 Equitable 包 >= v 0.6.0 read my article on Medium,对于旧版本或深入理解请阅读此答案。


当你父亲给你和你弟弟两件礼物时,两件礼物都是笔记本电脑,但它们不是同一型号的笔记本电脑;你想知道这两种礼物是否相等!因此,您将比较对您重要的所有方面 RAM、SSD、CPU。

论文:myLaptop: 16G/256G/i5 | myBrotherLaptop: 8G/512G/i5

假设你的大脑正在使用 Dart 语言,并且你认为每个礼物都是这个 class 的对象:

class LaptopGiftClass {
  int ram;
  int ssd;
  String cpu;

 // Default constructor
 LaptopGiftClass(this.ram, this.ssd, this.cpu);
}

然后比较使用上面 class 创建的两个礼物的相等性,Dart 和其他面向对象的语言,例如 Java,C# 期望您创建(覆盖)这些函数,以便使这些语言理解对象并能够比较相同 class:

的任意两个对象
@override
bool operator ==(Object myBrotherLaptop) =>
    identical(myLaptop, myBrotherLaptop) ||
    myBrotherLaptop is LaptopGiftClass &&
    runtimeType == myBrotherLaptop.runtimeType &&
    name == myBrotherLaptop.name;

@override
int get hashCode => name.hashCode;

如果这些台词吓到你了,没有人会责怪你,这就是为什么好心人为我们创造了 equatable package

Equatable 包 告诉你“把这个可怕的工作留给我”但是如何将可怕的代码委托给 equatable package??! 通过做两件事:

  1. 让你的 class extends equatable: dart class LaptopGiftClass extends Equatable {...}
  2. 从一开始就将需要与数组内部进行比较的所有属性传递给 Equatable(super/parent class),因此在构造函数内部:
LaptopGiftClass(this.ram, this.ssd, this.cpu) : super([ram, ssd, cpu]);

你的最终 class 是:

class LaptopGiftClass extends Equatable {
  int ram;
  int ssd;
  String cpu;

 // Default constructor
 LaptopGiftClass(this.ram, this.ssd, this.cpu) : super([ram, ssd, cpu]);

}

大功告成!您现在可以检查两个礼物是否相等,只需创建对象然后进行比较:

LaptopGiftClass myLaptop = LaptopGiftClass(16,256,'i5');
LaptopGiftClass myBrotherLaptop = LaptopGiftClass(8, 512,'i5');

就在开始比较之前,你的兄弟看到了你,因为他是一名游戏玩家,他希望你在此平等检查中添加更多属性:GPU 和 Screen_Resolution!你妈妈听说了,让你也加价格

现在您有一个要比较的新道具列表:[GPU,Screen_Resolution,价格]。

因此,因为您遵循简洁代码原则,所以您预料到了这一点,并且您使构造函数能够获得更多属性以与之进行比较:

// This only mean combine both lists
[ram, ssd, cpu]..addAll(myBrotherAndMotherProps) 

所以你的最终 class 是:

class LaptopGiftClass extends Equatable {
  int ram;
  int ssd;
  String cpu;

 // Default constructor
 LaptopGiftClass(
    this.ram, 
    this.ssd, 
    this.cpu, 
    // List of list => because we think "clean code" 
    // and maybe in the future we will send other data; NOT 
    // only an array(list)..
    // so we here sent the extra props we need to
    // compare 'myBrotherAndMotherProps', and 
    // as sometime brother and mother will not ask you 
    // to add props to compare, you give it a default value 
    // as empty "const []", why const here??! just for better 
    // performance as we are so soooo Professional!!

    [ List myBrotherAndMotherProps = const [] ],

 ) : super([ram, ssd, cpu]..addAll(myBrotherAndMotherProps));

 // WHY TO PASS FROM INSIDE THE CONSTRUCTOR? 
 // because Equatable needs them (required) 
 // and not at anytime but immediately inside the 
 // constructor of itself, so we made this 
 // chaining(constructor pass to another constructor)..
}

所以很明显,基本属性是 [RAM、SSD、CPU],但在我们使实施变得干净、灵活和可扩展时,也会考虑任何额外的属性。

在添加这个灵活的代码之前List<Object> get props => [RAM, SSD, CPU]..addAll(myBrotherAndMotherProps);这些曾经是相等的!!:

// Note first 3 are equal [ram, ssd, cpu]:
LaptopGiftClass myLaptop = LaptopGiftClass(16,256,'i5', ['Nvidia', 1080, '1200$']);
LaptopGiftClass myBrotherLaptop = LaptopGiftClass(16, 256,'i5', ['Intel HD', 720, '900$']);

myLaptop == myBrotherLaptop; // True without ..addAll(myBrotherAndMotherProps);
myLaptop == myBrotherLaptop; // False with ..addAll(myBrotherAndMotherProps);

TimerState 也发生了同样的情况:

@immutable
abstract class TimerState extends Equatable {
  final int duration;

  TimerState(this.duration, [List props = const []])
      : super([duration]..addAll(props));
}

TimerState 的实现与上面的 LaptopGiftClass 一样(最后一个实现)。 您可以使用构造函数将 props 发送给它:

TimerState(this.duration, [List props = const []])
      : super([duration]..addAll(props));

所以 TimerState 将在这一行中将 props 的列表传递给它的父级(super/ the Equatable / what extended..),如下所示: : super([duration]..addAll(props));

在这个计时器示例中; duration 是基本道具,就像 [RAM, SSD, CPU] 到 LaptopGiftClass。

层次结构将是这样的:

// Inside class Paused extends TimerState {...}
Paused(int duration) : super(duration); // super is TimerState

// then Inside abstract class TimerState extends Equatable {..}
TimerState(this.duration, [List props = const []])
      : super([duration]..addAll(props)); // super is Equatable

// Then Equatable will get props and deal with it for you...