In my app, I have a search function from Firestore. When I search a name in the TextField, I get what I want. When I delete the text (in the process of searching by using the backspace) and search again, I also get what I want. However when I clear the search button (by clicking on the clear Icon), and search again I don’t get any results. What did I do wrong?
MY CODE
TextEditingController searchController = TextEditingController();
@override
void dispose() {
super.dispose();
sideBarController.dispose();
}
bool isCleared = false;
List searchTerms = name.split(" ");
TextField(controller: searchController,
decoration: InputDecoration(
hintText: widget.searchHintString,
hintStyle: TextStyle(color: Colors.grey.shade500),
suffixIcon: searchController.text.isNotEmpty
? IconButton(onPressed: () {
setState(() {
searchController.clear();
name.isEmpty;
isCleared = true;
});
},
icon: const Icon(
Icons.clear_outlined,
olor: Colors.red,
),
): null,
),
onChanged: (value) {
setState(() {name = value;});
},
),
),
//For the ListTile (I didn't add the ListView.Builder here because I don't think it's relevant here. But I can add it if needed)
if (name.isEmpty || isCleared == true){
return ListTile(onTap: () {....},
title: ....//things I want to show
}
if (searchTerms.any((element) => dataUser["FirstName"].toString().startsWith(element)) || //userData defined in the StreamBuilder and ListViewBuilder
searchTerms.any((element) => dataUser["LastName"].toString().startsWith(element)) ||
searchTerms.any((element) => dataUser["OtherNames"].toString().startsWith(element)))
{return ListTile(onTap: () {....},
title: ....//things I want to show
}
else return const SizedBox();