簡體   English   中英

從 Firestore 獲取數據

[英]Getting Data from Firestore

所以我使用 Firebase 從 firestore 檢索數據,這工作正常,但現在為了節省資金和資源,我使用了 40 個項目的限制,所以只有 40 個項目來自 firebase,但現在當用戶到達列表末尾我希望用戶能夠從 firebase 數據庫中獲取接下來的 40 個項目。 這就是我不知道該怎么做。 無需從 firebase 中讀取全部 80 項即可獲得接下來的 40 項。

這是我的代碼:

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 將 cursor 移動到所需的 position

 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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM