Search ListView In flutter
Hello friends, today we will teach you how to search in list and listview.
Array List
List<Invoices> historyList = [];
if (response.statusCode == 200) {
var responseData = json.decode(response.body);
var orderHistory = responseData['invoices'];
for (var coinJSON in orderHistory) {
historyList.add(Invoices.fromJson(coinJSON));
items.add(Invoices.fromJson(coinJSON));
}
print('---------------->${historyList.length}');
print(responseData);
setState(() {});
}
var items = <Invoices>[];
void filterSearchResults(String query) {
List<Invoices> dummySearchList = <Invoices>[];
dummySearchList.addAll(historyList);
if (query.isNotEmpty) {
List<Invoices> dummyListData = <Invoices>[];
dummySearchList.forEach((item) {
if (item.swiggyOrderId!.contains(query)) {
dummyListData.add(item);
print("yes");
}
});
setState(() {
items.clear();
items.addAll(dummyListData);
});
return;
} else {
setState(() {
items.clear();
items.addAll(historyList);
});
}
}
Add method in Contoller
onChanged: (value) {
filterSearchResults(value);
setState(() {});
},
Comments
Post a Comment