gpt4 book ai didi

firebase - 从Firestore获取数据

转载 作者:行者123 更新时间:2023-12-03 03:33:18 26 4
gpt4 key购买 nike

因此,我正在使用Firebase从Firestore检索数据,并且工作正常,但是现在为了节省金钱和资源,我使用了40个项目的限制,因此只有40个项目来自Firebase,但是当用户到达在列表的末尾,我希望用户能够从Firebase数据库中获取接下来的40个项目。那就是我不知道该怎么做。要获取下40个项目,而不必从firebase中读取整个80个项目。
这是我的代码:

getCountryItems() async{
QuerySnapshot snapshot = await userCountry
.orderBy('timeStamp', descending: true)
.limit(40) //getting only 40 items
//.orderBy('likesCount', descending: true)
.getDocuments();

List<Items> countryPosts = snapshot.documents.map((doc) => Items.fromDocument(doc)).toList();
setState(() {
this.countryPosts = countryPosts;
});
}
这就是现在获取前40个项目的原因,我想仅在按下按钮后才能获取40个项目:
FlatButton(
onPressed: (){}//funtion to get the next 40 items
);

最佳答案

引用:https://firebase.google.com/docs/firestore/query-data/query-cursors
使用startAfter将光标移动到所需位置

getCountryItems(paginateAt = 0) async{
QuerySnapshot snapshot = await userCountry
.orderBy('timeStamp', descending: true)
.startAfter(paginateAt)
.limit(40) //getting only 40 items
//.orderBy('likesCount', descending: true)
.getDocuments();

List<Items> countryPosts = snapshot.documents.map((doc) => Items.fromDocument(doc)).toList();
setState(() {
this.countryPosts = countryPosts;
});
}

FlatButton(
onPressed: (){
getCountryItems(this.countryPosts.last)
}//funtion to get the next 40 items
);

关于firebase - 从Firestore获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62516206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com