代码之家  ›  专栏  ›  技术社区  ›  Stéphane de Luca

从“FutureBuilder”,我的未来永远不会返回数据

  •  0
  • Stéphane de Luca  · 技术社区  · 3 年前

    在future生成器中,我使用以下future,它应该返回文档的数量(集合存储广告详细视图的“已看到”事件):

    
     /// Stores and returns the document [id].
      static Future<int> getCount({required String? adId}) {
        if (adId == null) return Future<int>.value(0);
    
        // = Actually store the document
        final c = FirebaseFirestore.instance
            .collection(collectionName)
            .where("adId", isEqualTo: adId)
            .snapshots()
            .length;
    
        return c;
      }
    

    这个 FutureBuilder :

                        FutureBuilder(
                          future: ClassifiedAdSeen.getCount(adId: ad.id),
                          builder: (context, snapshot) {
                            if (snapshot.hasError) {
                              return const Icon(Icons.error);
                            } else if (!snapshot.hasData) {
                              return const CircularProgressIndicator();
                            }
                            final count = snapshot.data ?? "—";
                            return Text("$count");
                          }),
    

    但它从来没有 hasData 也没有 hasError ,旋转器继续前进。

    知道问题在哪里吗?

    [更新]

    通过获取文件,我可以毫无问题地完成和计数。 但是获取文档是在浪费内存和带宽,不是吗?

    那么,只获取文档计数的最佳方式是什么呢?

    
    /// Stores and returns the document [id].
      static Future<int> getCount({required String? adId}) async {
        if (adId == null) return 0;
    
        // = Actually store the document
        final c = await FirebaseFirestore.instance
            .collection(collectionName)
            .where("adId", isEqualTo: adId)
            .get(); 
    
        return c.size;
      }
    
    2 回复  |  直到 3 年前
        1
  •  1
  •   Gwhyyy    3 年前

    在您的情况下,只从查询中获取文档的长度,还有一个 count() 方法,返回 AggregateQuery ,它只返回文档的长度,而不返回其他内容。

    试试看:

    /// Stores and returns the document [id].
      static Future<int> getCount({required String? adId}) async {
        if (adId == null) return 0;
    
        // = Actually store the document
        final c = await FirebaseFirestore.instance
            .collection(collectionName)
            .where("adId", isEqualTo: adId)
            .count()
            .get(); 
    
        return c.count;
      }
    
        2
  •  0
  •   Gwhyyy    3 年前

    这个 snapshots() 返回一个 Stream ,既然您说您只想获得查询中的文档数,请考虑使用 get() ,此外,您还需要 async/await 在方法中,然后返回 QuerySnapshot 在您的 Future 方法,然后调用它的 docs 属性:

      static Future<QuerySnapshot> getQuerySnapshot({required String? adId})  async {
        
        final c = await FirebaseFirestore.instance
            .collection(collectionName)
            .where("adId", isEqualTo: adId)
            .get();    
        return c;
      }
    

    FutureBuilder :

    FutureBuilder<QuerySnapshot>(
                  future: ClassifiedAdSeen.getQuerySnapshot(adId: ad.id),
                  builder: (_, AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (snapshot.hasError) {
                      return const Icon(Icons.error);
                    } else if (snapshot.connectionState == ConnectionState.waiting) {
                      return const CircularProgressIndicator();
                    }
                    final count = snapshot.data.docs.length ?? "—"; // like this
                    return Text("$count");
                  }),