如何解决如何在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 举报,一经查实,本站将立刻删除。