问题描述
我正在下面使用带有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 (将#修改为@)