This is search address from cutnumid to show listview showing all address done and how can I want to search by typing only part of address? I’ve tried many different things and always crashes. please help me
//search custnum showaddress
onSearch(String text) async {
if (text.isNotEmpty) {
List<Item> itemList = [];
for (var item in items) {
if (item.custnum == text.toLowerCase().toUpperCase()) {
itemList.add(item);
setState(() {
searchitems.clear();
searchitems.addAll(itemList);
print('name : ${searchitems[0].name}');
if (searchitems.isEmpty) {
searchitems = [];
// print('searchitems : ${searchitems[0].address!.length}');
// print('searchitems : ${searchitems[0].address!}');
}
});
}
}
} else {
setState(() {
searchitems.clear();
// searchitems.addAll(items);
print('searchitems : $searchitems');
});
}
}
This is the code to search only for the words typed into the textfield. to display only searched This section may be invalid code. I gave an example.
onSearchAddr(String text) async {
if (text.isEmpty) {
setState(() {
loading = false;
});
return;
}
searchitems[0].address!.forEach((f) {
if (f.addr1.toString().toLowerCase().contains(text))
items.add(searchitems[0]);
});
}
2
Answers
You
setState()
on every single iteration. What if the iteration is faster thanbuild
method? it will crash, cause the app has to be redrawn when it is still redrawing.naive way to search by part of string. use
contains
method ofString
.The logic of your code seems to be working.
It could be errors from elsewhere such as unbounded
ListView