你好,我正在尝试在我的列表中使用分页,而列表数据来自Firebase。我不确定如何使用StreamBuilder对数据进行分页。现在我是通过在didChangeDependencies()函数中调用getdocuments来实现分页的。但是,在添加新数据后,列表没有更新。如果有人能帮助我,那就太好了。这是我现在正在做的事情...
didChangeDependencies() {
super.didChangeDependencies();
getProducts();
_scrollController.addListener(() {
double maxScroll = _scrollController.position.maxScrollExtent;
double currentScroll = _scrollController.position.pixels;
double delta = MediaQuery.of(context).size.height * 0.20;
if (maxScroll - currentScroll <= delta) {
getProducts();
}
});
}
getProducts() async {
if (!hasMore) {
return;
}
if (isLoading) {
return;
}
setState(() {
isLoading = true;
});
QuerySnapshot querySnapshot;
if (lastDocument == null) {
querySnapshot = await firestore
.collection('products')
.limit(documentLimit)
.orderBy('timestamp', descending: true)
.getDocuments();
} else {
querySnapshot = await firestore
.collection('products')
.startAfterDocument(lastDocument)
.limit(documentLimit)
.orderBy('timestamp', descending: true)
.getDocuments();
}
if (querySnapshot.documents.length < documentLimit) {
hasMore = false;
}
if (querySnapshot.documents.isNotEmpty) {
lastDocument =
querySnapshot.documents[querySnapshot.documents.length - 1];
products.addAll(querySnapshot.documents);
setState(() {
isLoading = false;
});
}
}
.listen((data) => onChangeData(data.documentChanges));
之前添加.limit(whatever_pagination_you_have)
,这样它就会减少你的读取量。这将只监听已添加的快照,而不是所有不必要的快照。 - rohan koshti