如何在Stream中实现分页?

问题描述

我正在下面使用带有Firestore数据库的Flutter应用程序中的listview构建器,

Stream stream = Firestore.instance.collection('users').snapshots();

我想对此流实施分页,类似于对Query query实施的以下分页,也可以正常工作。

Firestore _firestore = Firestore.instance;
List<DocumentSnapshot> _notifications = [];
bool _loadingnotifications = true;
DocumentSnapshot _lastDocument;
ScrollController _scrollController = ScrollController();
bool _loadingMore = false;
bool _moreProductsAvailable = true;

_getProducts() async {


  Query q = _firestore.collection("users").orderBy("timestamp",descending: true).limit(20);


  setState(() {

    _loadingnotifications = true ;
  });


  QuerySnapshot _querySnapshot = await q.getDocuments();
  

  _lastDocument = _querySnapshot.documents[_querySnapshot.documents.length - 1];

  _notifications = _querySnapshot.documents;
  setState(() {
    _loadingnotifications = false;
  });

}

_getMoreNotifications() async {
  print("new docs loaded");
  if(_moreProductsAvailable == false ){
    return;
  }

  if(_loadingMore == true ){

    return;
  }

  _loadingMore = true;
  Query q = _firestore.collection("users").orderBy("timestamp",descending: true).startAfter([_lastDocument.data['timestamp']]).limit(20);

  QuerySnapshot _querySnapshot = await q.getDocuments();

  if (_querySnapshot.documents.length < 20){
    _moreProductsAvailable = false;
  }
  _lastDocument = _querySnapshot.documents[_querySnapshot.documents.length - 1];

  _notifications.addAll(_querySnapshot.documents);

  _loadingMore = false;


}

我知道我可以将Stream替换为Query,但是,我只需要使用Stream是因为我需要根据某些条件自动更改查询,而我已经编码。 我在这里很难找到的问题就是要解决这个问题

      QuerySnapshot _querySnapshot = await q.getDocuments();
      _lastDocument = _querySnapshot.documents[_querySnapshot.documents.length - 1];
      _notifications = _querySnapshot.documents;

Stream。我还没有任何东西可以解决Stream的问题,如果可以解决,将实现分页。请进一步指导我如何解决此问题并使用Stream

进行分页

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...