Flutter Firestore Pagination of a Stream with or without startAfter?

Viewed 15

I've been working on a chat feature in my app and wanted to handle displaying messages with a paginated stream from firestore. Currently I'm using this code which is going through a provider. When the user scrolls, the provider adds pageNumber to the limit and then calls getMessages again. My main concern is that when I do this its reading all the docs again. For example on load the limit is 10, so it reads the first 10 docs, then after scrolling the limit is changed to 20, does it read the first 20, or does it just read the next 10 since it already has the first? I have looked though the firestore documentation and haven't been able to find a definitive answer to this. If anyone knows please let me know

If its the case it does re-read all docs, I will need to implement a startAfter, which isn't a big deal. I am just very curious about this and want to know definitively.

@override
  Stream<List<Message>> getMessages({
    required int limit,
    required String conversationID,
  }) {
    return firestore
        .collection(chatsCollectionPath)
        .doc(conversationID)
        .collection('messages')
        .orderBy('date', descending: true)
        .limit(limit)
        .snapshots()
        .map((event) => event.docs.map((e) => Message.fromDoc(e)).toList());
  }
0 Answers
Related