【问题标题】:Flutter Firestore count documents real-timeFlutter Firestore 实时计数文档
【发布时间】:2021-04-26 17:51:35
【问题描述】:

我的颤振项目中有一个代码,它计算集合中“已接受”为“待定”的文档数。

QuerySnapshot notifNum = await orders.where("id",isEqualTo:_auth.currentUser.uid).where("accepted",isEqualTo: "pending").get();

然后我列出 QuerySnapshot 中的所有文档并获取它的长度,以获取通知的数量:

List<DocumentSnapshot> _notifc = notifNum.docs;
int notifCount = _notifc.length;

但是,我想让它实时化,这样当文档从“待处理”更新为“已接受”时,通知的数量会自动更新。我尝试使用快照,但它们没有显示任何数字。我怎样才能让它发挥作用?

StreamBuilder<int>(
                        stream: _orders
                            .where("ustaId",isEqualTo:_auth.currentUser.uid)
                            .where("acceptedByUsta", isEqualTo: "pending")
                            .snapshots().length.asStream(),
                        builder: (context,snapshot) {
                          if(snapshot.hasError){
                            return Text("zzz");
                          }

                          if (snapshot.connectionState == ConnectionState.waiting) {
                            return Text("Loading");
                          }
                          if(snapshot.hasData){
                            print("aa");
                          }else{
                            print("zzz");
                          }
                          return new Positioned(
                                right: 0,
                                child: new Container(
                                  padding: EdgeInsets.all(1),
                                  decoration: new BoxDecoration(
                                    color: Colors.red,
                                    borderRadius: BorderRadius.circular(6),
                                  ),
                                  constraints: BoxConstraints(
                                    minWidth: 15,
                                    minHeight: 12,
                                  ),
                                  child: new Text(
                                    "aaa",
                                    style: new TextStyle(
                                      color: Colors.white,
                                      fontSize: 12,
                                    ),
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              );
                        }),

【问题讨论】:

  • 请注意,使用包含读取与查询对应的所有文档的解决方案,每次您想计算它们时,您都需要为所有这些文档的读取付费。如果您有很多文档并经常阅读计数器,这会迅速产生高成本。查看this answer 以查看基于计数器的另一种解决方案。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

按照这些步骤实现所需的功能。

  1. 您需要创建一个返回 documents 总数的流,其中键 'accepted' 的值为 'pending'。

  2. 将显示计数的小部件封装在StreamBuilder 内,或者在显示计数的小部件的“支架”上方的某处使用StreamProvider。。

  3. StreamBuilder 获取数据并显示在小部件上。

每当 Firestore 发生变化时,流自动更新 文件。

我已更改代码以满足您的需要。

  1. 返回计数的流

      Stream<QuerySnapshot> requestCount() {
          return orders
          .where("ustaId",isEqualTo:_auth.currentUser.uid)
          .where("acceptedByUsta", isEqualTo: "pending")
          .snapshots();
    
          }
    
  2. 使用 StreamBuilder

    StreamBuilder<QuerySnapshot>(
       stream: requestCount(),
       builder: (context, snapshot){
    
         if (snapshot.hasData){
    
           return Text(snapshot?.data.size.toString() ?? '0');
         }
         return Center(child: CircularProgressIndicator(),);
        }
       )
    

【讨论】:

  • 如何计算 Streambuilder 中的 DocumentSnapshot 数量?
  • @IBMR 刚刚编辑了我的答案并添加了详细信息以帮助您实现您想要的。
  • @IBMR 编辑了我的答案,因为您只想要QuerySnapshot 的长度。查看并提供您的反馈:)
  • 谢谢,但是流:requestCount 返回以下错误The argument type 'Stream&lt;int&gt; Function()' can't be assigned to the parameter type 'Stream&lt;int&gt;'.
  • 试过之前的代码,还是不行。编辑了它,现在它可以工作了,试试它并提供反馈:)
猜你喜欢
  • 2020-12-01
  • 2021-06-17
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2020-12-26
  • 2021-07-02
相关资源
最近更新 更多