I implemented an app where I search items in the sqlite database. The problem is caused when there are too many items in the database the app stops working in the physical device maybe because of limited ram. I use a list to fetch all the items in the database and then I use .where to search in that list for a particular string query. Can there be another way so that the app runs perfectly fine independent of the number of entries in the database.
this function matches the query with the list
this.filteredItemSuggestionList = (query == '')
? itemSuggestionList
: this
.itemSuggestionList
.where((p) =>
p.name.toLowerCase().contains(query.toLowerCase()) |
p.description.toLowerCase().contains(query.toLowerCase()) |
p.date.toLowerCase().contains(query.toLowerCase()))
.toList();
this function stores all the data in the database in the list
Future<void> getItemsFromDb() async {
final Future<Database> dbFuture = databaseHelper.initializeDatabase();
dbFuture.then((database) {
Future<List<ItemClass>> itemListFuture = databaseHelper.getItemList();
itemListFuture.then((itemList) {
this.itemSuggestionList = itemList;
//this.fullCount = itemList.length;
});
});
}
Future<List<ItemClass>> getItemList() async {
var itemMapList = await getItemMapList(); // get Map list from the database
int count = itemMapList.length;
List<ItemClass> itemList = List<ItemClass>();
for (int i = 0; i < count; i++) {
itemList.add(
ItemClass.fromMapObject((itemMapList[i])),
);
}
return itemList;
}