I am facing the same issue as well..I am going to provide my code and if someone who understands this problem, please help out. This has been this way since I started using Algolia.. for almost two years now.. I can never solve this issue.
here is lazy load class
class LazyLoadShortlists {
const LazyLoadShortlists(
this.alogliaShortlist, this.pageKey, this.nextPageKey);
final List<MdlShortlistAlgolia> alogliaShortlist;
final int pageKey;
final int? nextPageKey;
factory LazyLoadShortlists.fromResponse(SearchResponse response) {
final items = response.hits.map(MdlShortlistAlgolia.fromJson).toList();
final isLastPage = response.page >= response.nbPages;
final nextPageKey = isLastPage ? null : response.page + 1;
return LazyLoadShortlists(items, response.page, nextPageKey);
}
}
And here is my api call for Algolia
class AllShortlistRepository {
/// Component holding search filters
final filterState = FilterState();
// setup a page that display houseDatabase
final PagingController<int, MdlShortlistAlgolia> pagingController =
PagingController(firstPageKey: 0);
final searchTextController = TextEditingController();
// search houses in Algolia Database
final shortlistDatabase = HitsSearcher.create(
applicationID: AlgoliaCredentials.applicationID,
apiKey: AlgoliaCredentials.apiKey,
state: const SearchState(
indexName: AlgoliaCredentials.shortlistIndex,
numericFilters: ["createdTime >= 0"],
hitsPerPage: 10,
),
);
late final _agentNameFacet = shortlistDatabase.buildFacetList(
filterState: filterState, attribute: 'agentName');
late final _pinShortlistFacet = shortlistDatabase.buildFacetList(
filterState: filterState, attribute: 'pinShortlist');
AllShortlistRepository() {
shortlistDatabase.connectFilterState(filterState);
displayPropertiesOnThePage.listen((page) {
if (page.pageKey == 0) {
pagingController.refresh();
}
pagingController.appendPage(page.alogliaShortlist, page.nextPageKey);
}).onError((error) {
pagingController.error = error;
});
// this loads the list of house sucessfully properly when its enabled, but search does not work anymore
// but, when this disable, the search works, but it does not load the list of houses anymore
pagingController.addPageRequestListener((pageKey) =>
shortlistDatabase.applyState((state) => state.copyWith(page: pageKey)));
// pagingController.addPageRequestListener((pageKey) {
// shortlistDatabase.applyState((state) => state.copyWith(
// page: pageKey,
// ));
// });
filterState.filters.listen((_) => pagingController.refresh());
}
/// Get buyer list by query.
// void search(String query) {
// pagingController.refresh();
// shortlistDatabase.query(query);
// }
void search(String query) {
pagingController.refresh(); // Reset the PagingController state
shortlistDatabase.applyState((state) => state.copyWith(
query: query,
page: 0, // Reset to the first page to ensure a fresh search
facetFilters: [
'createdTime:${DateTime.now().millisecondsSinceEpoch}'
],
));
}
Future<List<ShortlistQuerySuggestion>> searchWithTypeAhead(
String query) async {
pagingController.refresh();
shortlistDatabase.query(query);
return suggestions.first;
}
// get stream of properties
Stream<LazyLoadShortlists> get displayPropertiesOnThePage =>
shortlistDatabase.responses.map(LazyLoadShortlists.fromResponse);
/// Get stream of search result, like the number of the properties
Stream<SearchMetadata> get searchMetadata =>
shortlistDatabase.responses.map(SearchMetadata.fromResponse);
// get stream of agentName
Stream<List<SelectableFacet>> get agentName => _agentNameFacet.facets;
// toggle agentName
void toggleAgentName(String agentName) {
pagingController.refresh();
_agentNameFacet.toggle(agentName);
}
/// Get stream of list of pinShortlist facets
Stream<List<SelectableFacet>> get priceRangeFacets =>
_pinShortlistFacet.facets;
/// Toggle selection of a priceRange facet
void togglePinShortlist(String pinShortlist) {
pagingController.refresh();
_pinShortlistFacet.toggle(pinShortlist);
}
/// Clear all filters
void clearFilters() {
pagingController.refresh();
filterState.clear();
}
Stream<List<ShortlistQuerySuggestion>> get suggestions =>
shortlistDatabase.responses.map((response) =>
response.hits.map(ShortlistQuerySuggestion.fromJson).toList());
/// Replace textController input field with suggestion
void completeSuggestion(String suggestion) {
searchTextController.value = TextEditingValue(
text: suggestion,
selection: TextSelection.fromPosition(
TextPosition(offset: suggestion.length),
),
);
}
/// In-memory store of submitted queries.
final BehaviorSubject<List<String>> _history =
BehaviorSubject.seeded(['jackets']);
/// Stream of previously submitted queries.
Stream<List<String>> get history => _history;
/// Add a query to queries history store.
void addToHistory(String query) {
if (query.isEmpty) return;
final _current = _history.value;
_current.removeWhere((element) => element == query);
_current.add(query);
_history.sink.add(_current);
}
/// Remove a query from queries history store.
void removeFromHistory(String query) {
final _current = _history.value;
_current.removeWhere((element) => element == query);
_history.sink.add(_current);
}
/// Clear everything from queries history store.
void clearHistory() {
_history.sink.add([]);
}
/// Dispose of underlying resources.
void dispose() {
shortlistDatabase.dispose();
}
}