🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Building Search UI / Ecommerce ui template / Components / Data sources / Search repository
On this page

Algolia for Flutter is beta software according to Algolia’s Terms of Service (“Beta Services”). To share feedback or report a bug, open an issue.

Code summary

The SearchRepository component provides convenient methods to run search queries, apply filters and display facets list. It encapsulates Flutter Helpers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class SearchRepository {
  /// Component holding search filters
  final _filterState = FilterState();

  /// Products Hits Searcher.
  late final _hitsSearcher = HitsSearcher(
    applicationID: Credentials.applicationID,
    apiKey: Credentials.searchOnlyKey,
    indexName: Credentials.hitsIndex,
  )..connectFilterState(_filterState);

  /// Brands facet lists
  late final _brandFacetList = FacetList(
    searcher: _hitsSearcher,
    filterState: _filterState,
    attribute: 'brand',
  );

  /// Size facet lists
  late final _sizeFacetList = FacetList(
    searcher: _hitsSearcher,
    filterState: _filterState,
    attribute: 'available_sizes',
  );

  /// Disposable components composite.
  final CompositeDisposable _components = CompositeDisposable();

  /// Search repository constructor.
  SearchRepository() {
    _components
      ..add(_filterState)
      ..add(_hitsSearcher)
      ..add(_brandFacetList)
      ..add(_sizeFacetList);
  }

  /// Set search page.
  void setPage(int page) {
    _hitsSearcher.applyState((state) => state.copyWith(page: page));
  }

  /// Get products list by query.
  void search(String query) {
    _hitsSearcher.query(query);
  }

  /// Get stream of latest search result
  Stream<SearchMetadata> get searchMetadata =>
      _hitsSearcher.responses.map(SearchMetadata.fromResponse);

  /// Get stream of latest search page
  Stream<ProductsPage> get productsPage =>
      _hitsSearcher.responses.map(ProductsPage.fromResponse);

  Stream<int> get appliedFiltersCount =>
      _filterState.filters.map((event) => event.getFilters().length);

  /// Get currently selected index
  Stream<SortIndex> get selectedIndex =>
      _hitsSearcher.state.map((state) => SortIndex.of(state.indexName));

  /// Update target index
  void selectIndexName(String indexName) {
    _hitsSearcher
        .applyState((state) => state.copyWith(indexName: indexName, page: 0));
  }

  /// Get stream of list of brand facets
  Stream<List<SelectableFacet>> get brandFacets => _brandFacetList.facets;

  /// Get stream of list of size facets
  Stream<List<SelectableFacet>> get sizeFacets => _sizeFacetList.facets;

  /// Toggle selection of a brand facet
  void toggleBrand(String brand) {
    _brandFacetList.toggle(brand);
    _hitsSearcher.applyState((state) => state.copyWith(page: 0));
  }

  /// Toggle selection of a size facet
  void toggleSize(String size) {
    _sizeFacetList.toggle(size);
    _hitsSearcher.applyState((state) => state.copyWith(page: 0));
  }

  /// Clear all filters
  void clearFilters() {
    _filterState.clear();
    _hitsSearcher.applyState((state) => state.copyWith(page: 0));
  }

  /// Dispose of underlying resources.
  void dispose() {
    _components.dispose();
  }
}
Did you find this page helpful?
Algolia for Flutter v0