felangel / equatable

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.

Home Page:https://pub.dev/packages/equatable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A Map considered equal even if its not

giacomomasseron opened this issue · comments

Describe the bug
My class:

class MyState extends Equatable {
  final Map<String, List<String>> selectedFilters;

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

Here I change the value of the property:

FutureOr<void> _event(...) {
        // DEEP COPY
	Map<String, List<String>> filters = Map.from(state.selectedFilters);

	if (!filters.containsKey(event.filter.key)) {
	  filters[event.filter.key] = [];
	}

	if (!filters[event.filter.key]!.contains(event.property.key)) {
	  filters[event.filter.key]!.add(event.property.key);
	} else {
	  filters[event.filter.key]!.remove(event.property.key);
	}

	emit(state.copyWith(selectedFilters: filters, filtersDirty: true));
}

Expected behavior
The classes are not equal.

Additional context
When I dispatch the event the first time, it works (the 2 classes are considered different). After the first time, they are considered equal.
If I remove the Equatable parent class, it works all the time.

@giacomomasseron You don't create a deep copy because you only copy the references of the lists. This way you also modify the list of the current state if !filters.containsKey(event.filter.key) does not occur. Therefore, this is not an Equatable bug.

@giacomomasseron Has this problem been solved by now?

@maeddin yes, thank you