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

Popular posts from this blog

Unlocking the Power of OOP: A Beginner's Guide to Objects, Encapsulation, Inheritance, Abstraction, and Polymorphism

HTTP GET Response in Flutter

Building a Flutter Firebase Firestore CRUD App