gpt4 book ai didi

flutter - Equatable 的子类将什么传递给 super (Equatable 类)?

转载 作者:行者123 更新时间:2023-12-03 02:43:33 24 4
gpt4 key购买 nike

嗨,我是 Flutter 的新手,我正在尝试了解 block 计时器 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));
}

最佳答案

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

当你父亲给你和你兄弟两件礼物时,两件礼物都是笔记本电脑,但不是同一类型的笔记本电脑;你想知道两个礼物是否相等!所以你会比较所有对你重要的方面内存、固态硬盘、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);
}

然后比较使用上面的类创建的两种礼物的相等性,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告诉你“把这个可怕的工作留给我” 但是如何将可怕的代码委托(delegate)给 equatable package ???
通过做两件事:
  • 让你的类扩展相等:dart class LaptopGiftClass extends Equatable {...}
  • 从一开始就将需要在数组中比较的所有属性传递给 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 和屏幕_分辨率 !你妈听了,让你加 价格 也!
    现在你有一个新的 Prop 列表来比较:[GPU,Screen_Resolution,Price]。
    因此,因为您遵循干净代码原则,所以您期望这样做,并且您使构造函数能够获得更多属性以进行比较:
    // 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将在这一行中将 Prop 的列表传递给它的父级( super /Equatable/扩展的..),如下所示: : super([duration]..addAll(props));在这个计时器示例中; duration是基本的 Prop ,就像 [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...

    关于flutter - Equatable 的子类将什么传递给 super (Equatable 类)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138791/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com