簡體   English   中英

我如何限制 Firebase 中帶有 Flutter 日期的文檔數量

[英]How do i limit the number of documents from Firebase with dates in Flutter

我正在構建一個 Flutter 應用程序,它通過 firebase cloud firestore 獲取文檔,但我希望它只顯示當天編寫的文檔。 就像我添加到集合中一樣,它只會返回今天的文檔。 請幫忙

Container(
              height: MediaQuery.of(context).size.height,
              child: StreamBuilder<QuerySnapshot>(
                  stream: Firestore.instance.collection('all').snapshots(),
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) {
                      return Text('.....Loading');
                    }
                    return ListView.builder(
                        scrollDirection: Axis.vertical,
                        itemCount: snapshot.data.documents.length,
                        itemBuilder: (context, index) {
                          DocumentSnapshot all = snapshot.data.documents[index];
                          return Container(
                           child: Row(
                             mainAxisAlignment: MainAxisAlignment.spaceAround,
                             children: [
                               Column(
                                 children: [
                                   Text('10:00', style: TextStyle(fontSize: 18),),
                                   SizedBox(height: 3,),
                                   Text('05 Sep', style: TextStyle(fontSize: 13),),
                                 ],
                               ),
                               Column(
                                 crossAxisAlignment: CrossAxisAlignment.start,
                                 children: [
                                   Text('Europa league', style: TextStyle(fontSize: 15),),
                                   SizedBox(height: 2,),
                                   Text( '${all['gg']['home']} vs ${all['gg']['away']}', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),

你只需要像這樣在查詢上放置一個 where 子句(這給出了過去 24 小時內所做的任何事情):

Firestore.instance
  .collection("all")
  .where("created_date", isGreaterThan: DateTime.now().subtract(Duration(days: 1)) )
  .snapshots();

確保您的數據具有日期,插入時您可以這樣做:

Firestore.instance
  .collection("all")
  .add({
    'data': "data",
    'created_date' : FieldValue.serverTimestamp(),
  });

暫無
暫無

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

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