刷新以加载新数据并滚动以分页数据Flutter Firestore [英] Refresh to load new data and scroll to paginate data Flutter Firestore

查看:41
本文介绍了刷新以加载新数据并滚动以分页数据Flutter Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"Feeds"的Firestore集合,其中包含给定用户的feed(例如Instagram)的帖子列表.视图加载后,我会按时间调用未来和订单

I have a Firestore collection called 'Feeds' that contains a list of posts for a given user's feed (like Instagram). When the view loads, I call a future and order by time

var userPostsDocumentSnapshot = await _feeds
          .document(userUid)
          .collection(items)
          .orderBy('postDateTime', descending: true)
          .getDocuments();

我最初使用的是流,但是当新数据添加到用户的feed中时,我不希望它自动显示,我希望下拉菜单加载新数据.但是,我还想通过在向下滚动(从最新到最旧)时对数据进行分页来提高效率,以使其更有效率.滚动时如何既能下拉加载新数据又能分页数据?

I was initially using a stream, but when new data is added to the user's feed, I don't want it to show automatically, I want a pull-down-to-load new data. However, I also want to make it efficient by paginating the data as you scroll down (from newest to oldest) to make it a little more efficient. How can I achieve both pull-down-to-load new data and also paginate data when scrolling?

推荐答案

对于下拉菜单,您只想再次执行查询.

For the pull-down-to-load, you'll just want to execute your query again.

对于分页,我使用了一个游标,如此处所述.例如,根据您使用UI的方式,您可以将查询限制为特定数量的文档.

For the pagination, I used a cursor, as documented here. For example, depending on how you're doing the UI, you can limit your query by a particular number of documents.

因此,对于您的查询,这样的操作可能会起作用:

So for your query, something like this may work:

var userPostsDocumentSnapshot = _feeds
          .document(userUid)
          .collection(items)
          .orderBy('postDateTime', descending: true)
          .limit(10);

然后,当您实际抓取文档时,请存储对最后一个文档的引用:

Then when you actually grab the documents, store a reference to the last one:

var docsnaps = await userPostsDocumentSnapshot.getDocuments();
var last = docsnaps.docs[documentSnapshots.docs.length-1];

然后在分页时,对查询使用一些细微变化:

Then when you paginate, use a slight variation on your query:

      ...  _feeds
          .document(userUid)
          .collection(items)
          .orderBy('postDateTime', descending: true)
          .startAfter(last);

这篇关于刷新以加载新数据并滚动以分页数据Flutter Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆