jaa*_*aaq 3 dart equatable flutter
我目前正在关注 resocoders clean 架构,在该架构中,他强烈建议使用Equatable来进行基本数据保存类,以便将来进行比较。问题是我的课程看起来像这样:
class Person extends Equatable {
final String name;
final List<AlterEgo> alterEgos;
Person({@required name, @required alterEgos}):super([name, alterEgos]);
@override
List<Object> get props => [name, alterEgos];
}
class AlterEgo extends Equatable {
final String name;
final String superPower;
AlterEgo({@required name, @required superPower}):super([name, superPower]);
@override
List<Object> get props => [name, superPower];
}
void main(){
Person("Clark", <AlterEgo>[AlterEgo("Superman", "Flying")]) == Person("Clark", <AlterEgo>[AlterEgo("Superman", "Flying")]) //true
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我编写构造函数时,IDE 会抱怨列表不可比较。我有点不知道现在要定义哪些函数/类来获取由原始类型组成的对象列表以与 equatable 一起使用。该包的文档似乎也省略了这个用例,仅声明 Equatable 仅适用于不可变类型。不过,我根本不介意该列表是不可变的。
编辑:完成的示例。目前在移动设备上,无法立即测试。
好吧,我的错误一定是在其他地方,在正确的 MWE 中使用列表似乎根本不是问题:
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
class Person extends Equatable {
final String name;
final List<AlterEgo> alterEgos;
Person({@required this.name, @required this.alterEgos});
@override
List<Object> get props => [name, alterEgos];
}
class AlterEgo extends Equatable {
final String name;
final String superPower;
AlterEgo({@required this.name, @required this.superPower});
@override
List<Object> get props => [name, superPower];
}
void main() {
print(Person(name: "Clark", alterEgos: <AlterEgo>[
AlterEgo(name: "Superman", superPower: "Flying"),
AlterEgo(name: "Spiderman", superPower: "Climbing"),
]) ==
Person(name: "Clark", alterEgos: <AlterEgo>[
AlterEgo(name: "Superman", superPower: "Flying"),
AlterEgo(name: "Spiderman", superPower: "Climbing"),
]));
print(Person(name: "Clark", alterEgos: <AlterEgo>[
AlterEgo(name: "Superman", superPower: "Flying"),
AlterEgo(name: "Spiderman", superPower: "Climbing"),
]) ==
Person(name: "Clark", alterEgos: <AlterEgo>[
AlterEgo(name: "Superman", superPower: "Flying"),
]));
print(Person(name: "Clark", alterEgos: <AlterEgo>[
AlterEgo(name: "Superman", superPower: "Flying"),
AlterEgo(name: "Spiderman", superPower: "Climbing"),
]) ==
Person(name: "Clark", alterEgos: <AlterEgo>[
AlterEgo(name: "Superman", superPower: "Flying"),
AlterEgo(name: "Spiderman", superPower: "Jumping"),
]));
}
Run Code Online (Sandbox Code Playgroud)
结果符合预期:
true
false
false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5425 次 |
| 最近记录: |