algolia / algolia/flutter-playground

Search input text does not generate result

Open
#17 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Hello, thank you for the tutorial.
I have been following this code, and I am facing a problem.
I can retrieve and display Algolia data on the screen sucessfully but when I type in the search input textFiled, I cannot generate results.

Please check out my code, tell me where I went wrong. Thank you.

Best,

```
class HousesListView extends StatefulWidget {
const HousesListView({Key? key}) : super(key: key);

@override
State createState() => _HousesListViewState();
}

class _HousesListViewState extends State {
// textController for search box input
final _searchTextController = TextEditingController();

// pageController from infinite_scroll_pagination package
final PagingController pagingController =
PagingController(firstPageKey: 0);

/// Component holding search filters from algolia_helper_flutter package
final _filterState = FilterState();

// search houses in Algolia Database
final _houseDatabase = HitsSearcher.create(
applicationID: AlgoliaCredentials.applicationID,
apiKey: AlgoliaCredentials.apiKey,
state: const SearchState(
indexName: AlgoliaCredentials.hitsIndex,
facetFilters: ['a2-propertyType: House']));

// stream and display list of properties on the screen
Stream get displayPropertiesOnThePage =>
_houseDatabase.responses.map(PropertiesPage.fromResponse);

/// Get stream of search result, like the number of the result from the search box
Stream get searchMetadata =>
_houseDatabase.responses.map(SearchMetadata.fromResponse);

@override
void initState() {
super.initState();

// listen to keystroke & query the results by the letters that user types in
_searchTextController
.addListener(() => _houseDatabase.query(_searchTextController.text));

// load properties on the page
displayPropertiesOnThePage.listen((properties) {
if (properties.pageKey == 0) pagingController.refresh();
pagingController.appendPage(
properties.alogliaPPT, properties.nextPageKey);
}).onError((error) => pagingController.error = error);

// error here!
// this loads the list of house successfully and 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) =>
_houseDatabase.applyState((state) => state.copyWith(page: pageKey))); //<= error occur in this line

// connect database and filter state
_houseDatabase.connectFilterState(_filterState);

// pageController listens to filterState
_filterState.filters.listen((_) => pagingController.refresh());
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBarTitle(context, 'List of Houses'),
backgroundColor: ZayyanColorTheme.zayyanGrey,
endDrawer: const Drawer(
width: 350,
child: HouseFilter(),
),
body: Center(
child: Column(
children: [
SizedBox(
height: 44,
child: TextField(
controller: _searchTextController,
decoration: const InputDecoration(
border: InputBorder.none,
hintText: 'Enter a search term',
prefixIcon: Icon(Icons.search),
),
),
),
StreamBuilder(
stream: searchMetadata,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox.shrink();
}
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text('${snapshot.data!.nbHits} hits'),
);
},
),
Expanded(
child: _hits(context),
),
],
),
),
);
}

Widget _hits(BuildContext context) {
return PropertyHitsListView(
pagingController: pagingController,
noItemsFound: (context) => const NoResultsView(),
onHitClick: (objectID) {
print(objectID);
},
);
}

@override
void dispose() {
_searchTextController.dispose();
_houseDatabase.dispose();
_filterState.dispose();
pagingController.dispose();
super.dispose();
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the HousesListView initState method, especially the TextEditingController listener, the displayPropertiesOnThePage stream, and the pagingController page-request callback using _houseDatabase.applyState. Reproduce the interaction with the search field and pagination, then verify that both the initial house list and updated search results continue to appear without the reported error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.