List of equatable objects as member of an equatable object

1,764

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
Share:
1,764
jaaq
Author by

jaaq

Updated on December 23, 2022

Comments

  • jaaq
    jaaq over 1 year

    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.

    • theboringdeveloper
      theboringdeveloper almost 4 years
      I think the props override is missing
    • jaaq
      jaaq almost 4 years
      Yes I omitted that for brevity. The problem isn't that I don't know how to use equatable itself, the problem arises when I need a List which is mutable with equatable which doesn't like lists.
    • julemand101
      julemand101 almost 4 years
      Can you add a complete example which we can copy into our own IDE and see the error in question?