B.G*_*ost 3 dart equatable flutter bloc
嗨,我是 flutter 中的 bloc 新手,我正在尝试了解块计时器在 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));
}
Run Code Online (Sandbox Code Playgroud)
Jeh*_*ser 35
更新:对于新版本的 Equitable 包>= v 0.6.0阅读我在 Medium 上的文章,对于旧版本或深入理解阅读这个答案。
当你父亲给你和你兄弟两件礼物时,两件礼物都是笔记本电脑,但它们不是同一类型的笔记本电脑;你想知道两个礼物是否相等!所以你会比较所有对你重要的方面RAM、SSD、CPU 的。
在纸上: 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);
}
Run Code Online (Sandbox Code Playgroud)
然后比较使用上述类创建的两种礼物的相等性,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;
Run Code Online (Sandbox Code Playgroud)
如果这些台词把你吓跑了,没有人责怪你,这就是为什么好心人为我们创造了平等的包裹!
Equatable 包告诉你“把这个可怕的工作留给我” 但是如何将可怕的代码委托给equatable 包??!通过做两件事:
dart class LaptopGiftClass extends Equatable {...} class LaptopGiftClass {
int ram;
int ssd;
String cpu;
// Default constructor
LaptopGiftClass(this.ram, this.ssd, this.cpu);
}
Run Code Online (Sandbox Code Playgroud)
你的最后一节课是:
@override
bool operator ==(Object myBrotherLaptop) =>
identical(myLaptop, myBrotherLaptop) ||
myBrotherLaptop is LaptopGiftClass &&
runtimeType == myBrotherLaptop.runtimeType &&
name == myBrotherLaptop.name;
@override
int get hashCode => name.hashCode;
Run Code Online (Sandbox Code Playgroud)
大功告成!您现在可以检查两个礼物的相等性,只需创建对象然后比较:
LaptopGiftClass(this.ram, this.ssd, this.cpu) : super([ram, ssd, cpu]);
Run Code Online (Sandbox Code Playgroud)
就在开始比较之前,你的兄弟看到了你,因为他是一名游戏玩家,他希望你在这个平等检查中添加更多属性:GPU 和 Screen_Resolution!你妈听了还叫你加价!
现在你有一个新道具列表来比较:[GPU, Screen_Resolution, Price]。
因此,因为您遵循干净的代码原则,所以您期望这样做,并且您使构造函数能够获得更多属性以进行比较:
class LaptopGiftClass extends Equatable {
int ram;
int ssd;
String cpu;
// Default constructor
LaptopGiftClass(this.ram, this.ssd, this.cpu) : super([ram, ssd, cpu]);
}
Run Code Online (Sandbox Code Playgroud)
所以你的最后一节课是:
LaptopGiftClass myLaptop = LaptopGiftClass(16,256,'i5');
LaptopGiftClass myBrotherLaptop = LaptopGiftClass(8, 512,'i5');
Run Code Online (Sandbox Code Playgroud)
所以很明显,基本属性是 [RAM、SSD、CPU],但是当我们使实现变得干净、灵活和可扩展时,也会考虑任何额外的东西。
在添加这个灵活的代码之前,List<Object> get props => [RAM, SSD, CPU]..addAll(myBrotherAndMotherProps);这些曾经是平等的!!:
// This only mean combine both lists
[ram, ssd, cpu]..addAll(myBrotherAndMotherProps)
Run Code Online (Sandbox Code Playgroud)
TimerState 也会发生同样的情况:
@immutable
abstract class TimerState extends Equatable {
final int duration;
TimerState(this.duration, [List props = const []])
: super([duration]..addAll(props));
}
Run Code Online (Sandbox Code Playgroud)
TimerState就像LaptopGiftClass上面一样实现(最后一个实现)。您可以props使用构造函数发送给它:
TimerState(this.duration, [List props = const []])
: super([duration]..addAll(props));
Run Code Online (Sandbox Code Playgroud)
所以 TimerState将在这一行中将 props 的列表传递给它的父级(super/ Equatable/ what extends..),如下所示:
: super([duration]..addAll(props));
在这个计时器示例中;duration是基本的道具,就像[RAM, SSD, CPU]LaptopGiftClass 一样。
层次结构将是这样的:
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)..
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6358 次 |
| 最近记录: |