Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to compare List<T extends Equatable> by elements ? #73

Closed
canisterism opened this issue Jun 24, 2020 · 1 comment
Closed

How to compare List<T extends Equatable> by elements ? #73

canisterism opened this issue Jun 24, 2020 · 1 comment

Comments

@canisterism
Copy link

canisterism commented Jun 24, 2020

Describe the question

I have a question about how to compare lists of instances extending Equatable.

Dart == for list does not compare their elements, but hashcode as you know.
So when you compare two instances even extending Equatable, the result would be always false.
The comparison does not depend on their values. This is an expected behavior I guess.

But I would like to know if there's any practice to do it. I'd appreciate it if you have any suggestions.

To Reproduce

class E extends Equatable {
  const E(this.value);
  
  final int value;

  @override
  List<Object> get props => [value];
}

final originalList = [E(1), E(1), E(1)];
final a = [...originalList];
final b = [...originalList];

a == b; // return false, expected to be true

Expected behavior

Expected the comparison to return true, comparing depending on their actual values, not hashcode.

Version

Dart version 2.8.4

Additional context

N/A

@canisterism
Copy link
Author

Sorry, this comment resolved my problem. I'm closing this issue.
#64 (comment)

class E extends Equatable {
  const E(this.value);
  final int value;
  @override
  List<Object> get props => [value];
}

class VM extends Equatable {
  const VM(this.e);
  final List<E> e;
  @override
  List<Object> get props => [e];
}

final originalList = [E(1), E(1), E(1)];
final a = [...originalList];
final b = [...originalList];

print(VM(a) == VM(b)); // true
print(VM(a).e == VM(b).e); // false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant