简体   繁体   中英

how can i define the last document of the list of posts?

This is how i fetch the postlist in a listview. This code fetches the initial list which is limited to 30 and I want to load more as the user scrolls. I want to fetch the next set of posts in the list that startAfter() the last Post in the initial list. How do i define the lastDocument for startAfter(lastDocument)?

static Future<List<Post>> getFeedPosts(String userId) async {
    QuerySnapshot feedSnapshot = await feedsRef
        .document(userId)
        .collection('userFeed')
        .orderBy('timestamp', descending: true)
        .limit(30)
        .getDocuments();
    List<Post> posts =
        feedSnapshot.documents.map((doc) => Post.fromDoc(doc)).toList();
    return posts;
  }

Whenever you load your document list save it inside a variable and you can get the last item using:

yourDocumentList.last;

or you can simply save your last item

feedSnapshot.documents.last;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM