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

Different hashCodes produced for classes with List properties #8

Closed
georgepapas opened this issue Feb 26, 2019 · 5 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@georgepapas
Copy link

Describe the bug
When a class extends Equatable and that class has a list property, two different instances, with the exact same properties, produce different hashCode values.

To Reproduce

class Foo extends Equatable {
  String foo;
  String bar;
  List<String> list;

  Foo(this.foo, this.bar, this.list) : super([foo, bar, list]);
}

void main() {
  group("hashCode test", () {
    test("foo", () {
      expect(Foo('aaa', 'bbb', []).hashCode, equals(Foo('aaa', 'bbb', []).hashCode));
    });
  });
}

Expected behavior
expected the same hashCode to be produced

Version
0.2.1

@felangel felangel self-assigned this Feb 26, 2019
@felangel felangel added the bug Something isn't working label Feb 26, 2019
@felangel felangel mentioned this issue Feb 26, 2019
3 tasks
@felangel
Copy link
Owner

@georgepapas thanks for reporting this! I'm pushing a fix for it now in #9 👍

@felangel
Copy link
Owner

@georgepapas this should now be fixed in equatable v0.2.2. Thanks again for catching and reporting this! 💯

@georgepapas
Copy link
Author

Thanks for that, that was quick!, I'll take a look. Just one thing about the change, I ended up using something like this locally as a temporary workaround

DeepCollectionEquality.unordered().equals(my_list, other.my_list);

Does the fix in the commit introduce potential for stack overflow error if you have a list with items that reference the list?

Cheers

@felangel
Copy link
Owner

@georgepapas thanks! Hmm can you provide an example? 🤔

@georgepapas
Copy link
Author

Hey @felangel . Sorry for the late reply... Try this...

class Child extends Equatable {
  Parent parent;
  Child(this.parent) : super([parent]);
}

class Parent extends Equatable {
  List<Child> children;
  Parent(List<Child> children) : super([children]);

  void addChild(Child child) {
    children.add(child);
  }
}

void main() {
  Parent parent;

  setUp(() {
    List<Child> children = [];
    parent = Parent(children);

    children.add(Child(parent));
    children.add(Child(parent));

  });

  test("biderectional relationships cause stack overflow", () {
    parent.hashCode;
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants