I’m trying to search through List<Map<String,dynamic>> partyList where each map stores information about party. I implemented searchfield as follows.
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: SearchField<Map<String, dynamic>>(
onSearchTextChanged: (query) {
final filter = partyListDoc
.where((element) =>
element['partyName'].toLowerCase().contains(query.toLowerCase()))
.toList();
return filter
.map((party) =>
SearchFieldListItem<Map<String, dynamic>>(
party['partyName'],
item: party,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(party['partyName']),
Text(
party['currentBalance'].toString(),
style: TextStyle(
color: party['currentBalance'] < 0 ? Colors.red : Colors.green,
),
),
],
),
))
.toList();
},
suggestions: partyListDoc.map((party) {
return SearchFieldListItem<Map<String, dynamic>>(
party['partyName'],
item: party,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(party['partyName']),
Text(
party['currentBalance'].toString(),
style: TextStyle(
color: party['currentBalance'] < 0 ? Colors.red : Colors.green,
),
),
],
),
);
}).toList(),
suggestionState: Suggestion.expand,
textInputAction: TextInputAction.next,
hint: 'Search by Party Name',
searchStyle: TextStyle(
fontSize: 18,
color: Colors.black.withOpacity(0.8),
),
validator: (x) {
if (x == null || x.isEmpty) {
return 'Please enter a party name';
}
return null;
},
searchInputDecoration: InputDecoration(
border: OutlineInputBorder(),
),
maxSuggestionsInViewPort: 6,
itemHeight: 50,
onSuggestionTap: (SearchFieldListItem<Map<String, dynamic>> suggestion) {
setState(() {
selectedPartyId = suggestion.item!['partyId'];
});
focus.unfocus();
},
focusNode: focus,
)
),
after typing text in the search field, it filters the results once for a short while and then displays the whole list again. How to just display the filtered results in the suggestions.
I have tried removing and modifying onSearchTextChange, but the results are same.
New contributor
Shrikant Agrawal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.