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
22 stars 15 forks source link

[Filter Snapshot]: Unable to read the 'value' value from the filter snapshot. #76

Open Patrick386 opened 1 year ago

Patrick386 commented 1 year ago

Unable to read the 'value' value from the filter snapshot.

 final FilterState _filterState = FilterState();
   Filters filterList = _filterState.snapshot();
   Set<Filter> fs = filterList.getFilters();
logger.info('filter>>>>:${fs.toString()}');

value: product INFO: filter>>>>:{FilterFacet{attribute: type, isNegated: false, value: product, score: null}}

I want to display the selected filter value list in the UI, but I am unable to read the 'value'.

VladislavFitz commented 1 year ago

Hi @Patrick386 ,

Thank you for bringing up the issue you're facing with reading the value from the Filter class. I'd be happy to assist you with this.

The reason you're unable to access the value is because Filter is an abstract class. As such, it serves as a base class for other concrete filter implementations, namely FilterFacet, FilterNumeric, and FilterTag. Each of these implementations provides a different type of value.

To resolve this issue, you need to explicitly cast your Filter object to the appropriate concrete filter type. By doing so, you will gain access to the specific value associated with that particular filter implementation.

For example, if you have a Filter object and you know that it is an instance of FacetFilter, you can cast it as follows:

Filter filter = ... // Your Filter object
if (filter is FilterFacet) {
  FilterFacet facetFilter = filter as FilterFacet;
  // Now you can access facetFilter.value
}

Similarly, you can perform the cast for NumericFilter or TagFilter based on the specific implementation you are working with. I hope this helps! If you have any further questions or need additional assistance, please let me know.

Patrick386 commented 1 year ago

Hi @Patrick386 ,

Thank you for bringing up the issue you're facing with reading the value from the Filter class. I'd be happy to assist you with this.

The reason you're unable to access the value is because Filter is an abstract class. As such, it serves as a base class for other concrete filter implementations, namely FilterFacet, FilterNumeric, and FilterTag. Each of these implementations provides a different type of value.

To resolve this issue, you need to explicitly cast your Filter object to the appropriate concrete filter type. By doing so, you will gain access to the specific value associated with that particular filter implementation.

For example, if you have a Filter object and you know that it is an instance of FacetFilter, you can cast it as follows:

Filter filter = ... // Your Filter object
if (filter is FilterFacet) {
  FilterFacet facetFilter = filter as FilterFacet;
  // Now you can access facetFilter.value
}

Similarly, you can perform the cast for NumericFilter or TagFilter based on the specific implementation you are working with. I hope this helps! If you have any further questions or need additional assistance, please let me know.

I need a page with a fixed menu order. My question was about creating a menu selector. For example, I have the following menu items: 'All, Music, News, Interior, Nature'. If there is no data available for a selected menu, it should display an empty page. Each menu item is implemented as a single toggle, and for 'All', I have implemented it using the method '_filterState.clear();'. I have already implemented it, but if there are any recommended approaches, please let me know.

I also needed a menu selector with a multi-toggle approach that required the 'filter value' of the searched data. I have implemented it by reading the manual.

Thank you in advance.

스크린샷 2023-06-10 오후 5 47 47