algolia / algolia/algoliasearch-helper-flutter

HitSearcher is fetching data from Algolia already when just initially adding a stream listener

Open
#122 3 comments 3 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
27
Forks
21
Avg merge
1h 19m
Merged PRs (30d)
1

Description

### **Describe the bug 🐛**

**TL;DR:**
Algolia backend gets called with a search immediately after adding a listener function to the response stream, even before actively starting a search by executing the `hitsSearcher.query(String query)` method.

**Long version:**

I'm following [Getting started with Flutter Helper](https://www.algolia.com/doc/guides/building-search-ui/getting-started/flutter/), but want to use `algolia_helper_flutter` without `infinite_scroll_pagination` and following clean architecture using the BLoC pattern.

I'm initializing the HitsSearcher in my BLoC constructor the common way like this:
```dart
_productsSearcher = HitsSearcher(applicationID: 'latency',
apiKey: '927c3fe76d4b52c5a2912973f35a3077',
indexName: 'STAGING_native_ecom_demo_products');
```

I also want to add a listener to the responses stream as soon as responses are coming in after queries.

I can do that with
```dart
_productsSearcher.responses.listen(_searchPageListener);
```

⚠️ **As soon as I call `listen()` to add the listener function, HitsSearcher executes a call to the Algolia backend. This I don't want, because I'm still in the setup phase in my BLoC constructor.**

This call is happening because when accessing the responses stream, this code is executed: `searchService.search(req.state)`:
https://github.com/algolia/algoliasearch-helper-flutter/blob/0b0154a5aed1a198239cc957ab05a68abbbe1d2a/helper/lib/src/searcher/hits_searcher.dart#L272

..which in turn executes this:
https://github.com/algolia/algoliasearch-helper-flutter/blob/0b0154a5aed1a198239cc957ab05a68abbbe1d2a/helper/lib/src/service/algolia_hits_search_service.dart#L77

### **To Reproduce 🔍**

1. Initialize HitsSearcher()
2. Add a listener function to the response stream
3. :x: See listener function being called immediately, even without a call to hitsSearcher.query() before.

### **Expected behavior 💭**
Algolia search does not get called immediately when adding the response listener, but only after calling searcher.query()

### **Environment:**

- OS: Android API 33
- Library Versions
- algolia_helper_flutter: 0.5.0 and algolia_helper_flutter: **^1.0.0-pre** as well

### **Additional context**
Thanks for supporting Flutter ❤️

Contributor guide

Open the contributing guide

Research direction

Start in helper/lib/src/searcher/hits_searcher.dart at the responses stream behavior, then inspect helper/lib/src/service/algolia_hits_search_service.dart where the request is sent. Verify that adding a listener does not call Algolia before HitsSearcher.query(), while an explicit query still performs the search.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
search
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.