作为可等化对象成员的可等化对象列表

List of equatable objects as member of an equatable object

我目前正在关注 resocoders clean architecture in which he highly recommends to use Equatable 以获取基本数据保存 classes,将来会进行比较。问题是我的 class 看起来像这样:

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
}

问题是,当我编写构造函数时,IDE 抱怨列表不可比较。我有点不知道现在要定义哪个 functions/classes 来获取由基本类型构成的对象列表,以便与 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"),
      ]));
}

结果符合预期:

true
false
false