I am pulling 3000 items (these are constants and can't be edited) from Firestore, saving them locally on the first load and displaying them as database in the app where users can pick items from the list and add them to their personal screen in app. Initial list of items in database will show plus icon for each item as the trailing button. When user taps the plus icon, it gets added to the personal collection in Firestore and to personal screen in the app and the icon changes to check. Everything seems to be working fine but the icon change is taking too long for 3000 list items (it's fast with 500 items).
The way i'm checking to see if the item is added is by comparing item id. When user adds the item from the database, everything is copied to their personal collection including the id. Now in the app, i check to see if the database item id matches the personal item id and if they do, display check button and if not display plus button. This process is really slow (5-7 secs just to add and display check) for 3000 items and i was wondering if there is a better way to handle this. Below is the pseudo cod, work restricts me from sharing the code or any identifiable info but please let me know if you need any additional info. Thanks in advance.
Note: This issue is in Release mode. Debug mode is way way slower.
List<String> personalItemIds = ...;
ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int itemIndex) {
DBModel dataFromFirestore = snapshot.data[itemIndex];
bool itemAdded = personalItemIds.contains(dataFromFirestore.id);
return Card(
child: ListTile(
leading: Icon(...)
),
title: Text(..),
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: itemAdded ? Icon(
FontAwesomeIcons.checkCircle,
color: Colors.green,
) : Icon(
FontAwesomeIcons.plusCircle,
color: Colors.blue),
onPressed: () async {
if (!itemAdded) {
FirestoreCollec...addItem(..).then((_){
print('added');
itemStatusChange = true;
personalItemIds.add(dataFromFirestore.id);
Toast.show('Added!', context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
setState(() {});
});
} else {
FirestoreCollec....deleteItem(...).then((_){
print('deleted');
itemStatusChange = true;
personalItemIds.remove(dataFromFirestore.id);;
Toast.show('Removed!', context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
setState(() {});
});
}
})
],
),
));
},
)