algolia / algoliasearch-helper-flutter

⚡️ Building block to create instant-search applications with Flutter
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/flutter/
Other
21 stars 14 forks source link

Toggle 2 facets with forEach doesn't listen both changes #109

Closed bichoalexis closed 6 months ago

bichoalexis commented 8 months ago

Describe the bug 🐛
When you try to toggle a facet almost at the same time that the previous one, the filter state who listen filters, detects only one instead of adding the second one.

To Reproduce 🔍
Steps to reproduce the behavior:

  1. Create a list of facets to toggle
  2. Toggle facets which are only different than the previous one

Expected behavior 💭
Both Facets should be added

Here is an example of my code:

  var _previousFacets = <SelectableFilter>[];

  @override
  void setFacet(List<SelectableFilter> facet) {
    if (_previousFacets.isEmpty) {
      _previousFacets = facet;
      facet.where((element) => element.selected).forEach(
        (element) {
          _facetList.toggle(element.name);
        },
      );
      return;
    }

    facet.asMap().forEach((index, currentElement) {
      final previousElement = _previousFacets[index];
      if (previousElement.selected != currentElement.selected) {
        _facetList.toggle(currentElement.name);
      }
    });

    _previousFacets = facet;
  }

And here is what the listener of _filterState.filters prints

flutter: Filters{facetGroups: {FilterGroupID{ name: containedBranches, operator: FilterOperator.or}: {FilterFacet{attribute: containedBranches, isNegated: false, value: FILTER1, score: null}}}, tagGroups: {}, numericGroups: {}, hierarchicalGroups: {}}
flutter: Filters{facetGroups: {FilterGroupID{ name: containedBranches, operator: FilterOperator.or}: {FilterFacet{attribute: containedBranches, isNegated: false, value: FILTER2, score: null}}}, tagGroups: {}, numericGroups: {}, hierarchicalGroups: {}}

Instead of: flutter: Filters{facetGroups: {FilterGroupID{ name: containedBranches, operator: FilterOperator.or}: {FilterFacet{attribute: containedBranches, isNegated: false, value: FILTER1, score: null}, FilterFacet{attribute: containedBranches, isNegated: false, value: FILTER2, score: null}}}, tagGroups: {}, numericGroups: {}, hierarchicalGroups: {}}

Youssef-Sholkamy commented 7 months ago

Can confirm, same issue here.