Home > front end >  How do I ensure that the list is populated before checking what it contains?
How do I ensure that the list is populated before checking what it contains?

Time:01-14

In the code snippet below, how do I ensure that the senderID list gets populated before checking what the list contains? I'm pulling data from firestore, storing it in a list, and then checking if it contains a particular key.

      Stream<List<Group>> getChatGroups(String grpId) {
        return firestore.collection('groups').snapshots().map((event) {
          List<Group> groups = [];
          List<String> senderIds = [];
    
          for (var document in event.docs) {
            var group = Group.fromMap(document.data());
    
            firestore.collection('groups').doc(group.groupId).collection('chats').get().then((QuerySnapshot snapshot) {
              snapshot.docs.forEach((DocumentSnapshot doc) {
                var messageData = doc.data() as Map<String, dynamic>;
                var messages = Message.fromMap(messageData);
                var grId = doc.reference.parent.parent?.id;
    
    //The values in SenderIds should be set before the function below is initiaited 
                senderIds.add(messages.senderId);
    
              });
              
            });
    
    
    //This if function should initiate after getting set above
            if (senderIds.contains(auth.currentUser!.uid)) {
              groups.add(group);
            }
          }
          return groups;
        });
      }

CodePudding user response:

If you want senderIds.contains to be called only after all of your Futures have completed, build a List of those Futures and use Future.wait on that List. Something like:

var futures = <Future<void>>[];
for (var document in event.docs) {
  // ...

  futures.add(
      firestore
        .collection('groups')
        .doc(group.groupId)
        .collection('chats')
        .get()
        .then((QuerySnapshot snapshot) {
           // ...
        }),
  );
}

await Future.wait(futures);
if (senderIds.contains(auth.currentUser!.uid)) {
  // ...
}

Note that since the above code is asynchronous, you should also be using asyncMap instead of map.

  • Related