0

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(() {});
                                });                        
                            }
                          })
                    ],
                  ),
                ));
              },
            )
droider
  • 11
  • 2
  • You to provide code for a minimal reproducable example. Strip out things that aren't relevant to your question. – Christian Feb 25 '20 at 16:58
  • Sounds like you are processing the two lists directly from the database? It is much quicker to load the two lists into memory in the app and then compare them there. – GrahamD Feb 25 '20 at 16:58
  • Loading such a huge amount of data is not memory sufficient for the mobile devices. You need to paginate the data records while loading. – Kiran Maniya Feb 25 '20 at 17:08
  • @Christian code added. Thanks. – droider Feb 25 '20 at 17:14
  • @GrahamD, actually i am saving the database locally on the first load, sorry forgot to add that info. – droider Feb 25 '20 at 17:15
  • @KiranManiya i thought about pagination but i'm worried about it slowing down user experience. – droider Feb 25 '20 at 17:22
  • @droider Pagination doesn't slow down the app or give bad user experience at all. Most apps use pagination in infinite list/grids, consider Pinterest app or Facebook it self. Check this SO post https://stackoverflow.com/questions/53114867/infinite-list-in-flutter-application – Kiran Maniya Feb 25 '20 at 17:30
  • @kiranManiya with due respect, 3000 items is hardly a huge amount of data. 1MB equates to 1 million characters. I would like to know from the OP how many characters there are in his 3000 items. I would guess it is less than one million. – GrahamD Feb 25 '20 at 18:10
  • @KiranManiya, thanks for the info but based on my understanding with pagination (please correct me if i'm wrong), seems like i need to know the size of the list beforehand which may not be possible in my case as it could change remotely (in firestore). – droider Feb 25 '20 at 19:16
  • @GrahamD, agreed. a list of 3000 items is not a huge list. Each item has about 100 sub key/value pairs (with one value of may be 100-150 characters, rest are all numerical values of 4 chars max) – droider Feb 25 '20 at 19:22
  • @droider I am really struggling to see why your code leads to such delays in the icon being changed. Maybe try setting a timer and printing out durations in the item builder. You need to home in somehow. Would be good to know how long the .contains takes. I haven't used physics and shrinkwrap so don't know if they have an effect. Maybe comment them out at this stage. – GrahamD Feb 25 '20 at 19:37
  • The 100 sub key/value pairs is interesting. Are u sure you are efficiently comparing the two lists? You maybe need to print out what you are actually comparing? – GrahamD Feb 25 '20 at 19:41

0 Answers0