嗨,我是bloc的新手,我试图理解块计时器In the doc of flutter_bloc,我想知道这个构造函数类是什么意思
@immutable
abstract class TimerState extends Equatable {
final int duration;
//and this list props ??
TimerState(this.duration, [List props = const []])
: super([duration]..addAll(props));
}
答案 0 :(得分:7)
当您的父亲给您和您的兄弟2件礼物,而这两种礼物都是笔记本电脑,但它们不是同一类型的笔记本电脑时,您想知道这两种礼物是否相等!因此,您将比较对您而言重要的所有方面
在论文上:myLaptop: 16G/256G/i5 | myBrotherLaptop: 8G/512G/i5
假设您的大脑正在使用Dart语言,并且您将每种礼物都视为此类的对象:
class LaptopGiftClass {
int ram;
int ssd;
String cpu;
// Default constructor
LaptopGiftClass(this.ram, this.ssd, this.cpu);
}
然后比较使用上述类,Dart和其他面向对象的语言(例如Java,C#)创建的所有礼物的均等性,希望您创建(覆盖)这些功能,以使这些语言理解对象并能够比较相同类别的任何两个对象:
@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 package告诉您“把这个可怕的工作留给我”,但是如何将可怕的代码委托给equatable package? 通过做两件事:
dart class LaptopGiftClass extends Equatable {...}
LaptopGiftClass(this.ram, this.ssd, this.cpu) : super([ram, ssd, cpu]);
您的最后一堂课是:
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,屏幕分辨率,价格]。
因此,由于您遵循简洁的代码原理,因此可以预期,并且使构造函数能够获得更多与之比较的属性:
// This only mean combine both lists
[ram, ssd, cpu]..addAll(myBrotherAndMotherProps)
所以您的最后一堂课是:
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 / Equatable /扩展的对象..),如下所示:
: super([duration]..addAll(props));
,在此计时器示例中; duration
是基本道具,就像LaptopGiftClass的[RAM, SSD, CPU]
一样。
,层次结构将如下所示:
// 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...
答案 1 :(得分:0)
本教程使用equatable: ^0.2.0
,在此版本中,当您要覆盖hashcode
和==
运算符时,需要将List
个属性传递给{{ 1}}构造函数。签出docs。
为此,他创建了一个名为super
的可选参数,并将包含props
和List
参数的所有元素的duration
传递给超级构造函数
props
通过这种方式,扩展abstract class TimerState extends Equatable {
final int duration;
TimerState(this.duration, [List props = const []])
: super([duration]..addAll(props));
}
的类可以使用TimerState
可选参数来传递其他属性,并且此属性将添加到传递给{{ {}}的1}}构造函数,以正确使用props
。
因此,如果您需要一个具有其他属性的州,则需要这样做:
List