Search code examples
flutterdartequatable

List of equatable objects as member of an equatable object


I am currently following resocoders clean architecture in which he highly recommends to use Equatable for basic dataholding classes that will be compared in the future. The thing is my class will look something like this:

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
}

The thing is when I write the constructors the IDE complains that lists aren't comparable. I am kind of lost as to which functions/classes to define now to get a list of objects made from primitive types to work with equatable. The documentation of the package also seems to omit this usecase, only stating that Equatable only works with immutable types. However I wouldn't mind the list being immutable at all.

EDIT: completed example. On mobile right now, cannot test it right away.


Solution

  • Okay my error must be somewhere else, using lists doesn't seem to be a problem at all in a correct 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"),
          ]));
    }
    
    

    result as expected:

    true
    false
    false