【问题标题】:How to get the number of Firestore documents in flutter如何在flutter中获取Firestore文档的数量
【发布时间】:2019-04-20 12:20:31
【问题描述】:

我正在构建一个颤振应用程序并使用 Cloud Firestore。我想获取数据库中所有文档的数量。

我试过了

Firestore.instance.collection('products').toString().length

但是没有用。

【问题讨论】:

    标签: firebase dart flutter google-cloud-firestore


    【解决方案1】:

    应该是 - Firestore.instance.collection('products').snapshots().length.toString();

    【讨论】:

    • 请注意,这将导致客户端下载集合中的所有文档只是为了获取计数。对于大型馆藏,这可能效率极低且成本高昂。
    • @DougStevenson,获得计数的最佳方法是什么?
    • @Olantobi 推荐的解决方案在文档中。 firebase.google.com/docs/firestore/solutions/counters
    • @DougStevenson 是计数器方法类似于递增计数,每当添加一个孩子?
    • 这会返回一个Future类型,那么你怎么做呢?在您提供的示例中,它甚至没有作为字符串有价值,因为它只是显示错误,并且字符串的值是 "Instance of Future"
    【解决方案2】:

    Firebase 并没有官方提供任何函数来检索集合中的文档数量,而是您可以获取集合的所有文档并获取它的长度..

    有两种方式:

    1)

    final int documents = await Firestore.instance.collection('products').snapshots().length;
    

    这将返回一个 int 值。但是,如果你不使用 await,它会返回一个 Future。

    2)

    final QuerySnapshot qSnap = await Firestore.instance.collection('products').getDocuments();
    final int documents = qSnap.documents.length;
    

    这将返回一个 int 值。

    但是,这两种方法都会获取集合中的所有文档并对其进行计数。

    谢谢

    【讨论】:

    • 我认为这行不通......因为它会返回未来的int......你在streambuilder或future builder中需要它。请分享完整的sn-p
    【解决方案3】:

    首先,您必须从该集合中获取所有文档,然后您可以通过文档列表获取所有文档的长度。下面应该可以完成工作。

    Firestore.instance.collection('products').getDocuments.then((myDocuments){
     print("${myDocuments.documents.length}");
    });
    

    【讨论】:

    • 如果每个集合中有数千个文档怎么办?
    【解决方案4】:

    由于您正在等待未来,因此必须将其放在异步函数中

    QuerySnapshot productCollection = await 
    Firestore.instance.collection('products').get();
    int productCount = productCollection.size();
    

    集合中的文档数量

    【讨论】:

      【解决方案5】:
       Future getCount({String id}) async => FirebaseFirestore.instance
            .collection(collection) //your collectionref
            .where('deleted', isEqualTo: false)
            .get()
            .then((value) {
          var count = 0;
          count = value.docs.length;
      
          return count;
        });
      

      这是飞镖语言...

      【讨论】:

        【解决方案6】:
        Firestore.instance
            .collection("products")
            .get()
            .then((QuerySnapshot querySnapshot) {
          print(querySnapshot.docs.length);
        });
        

        【讨论】:

        • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票
        【解决方案7】:

        以上建议将导致客户端下载集合中的所有文档以获取计数。作为一种解决方法,如果您的写入操作不经常发生,您可以将文档的长度放到 Firebase 远程配置中,并在您从 Firestore 集合中添加/删除文档时更改它。然后,您可以在需要时从 firebase 远程配置中获取长度。

        【讨论】:

        • 如何使用远程配置来做到这一点?
        • 此解决方案很容易不同步(由于更新失败或应用程序端的事务隔离格式错误......),获取真实数据(如选择计数)始终是一个好习惯where 子句或 Firestore.instance.collection('products').snapshots().length; (snapshots() 是一个流,如果您不想下载,不会导致下载数据库中的所有文档)
        猜你喜欢
        • 2020-11-08
        • 2019-05-05
        • 2020-12-05
        • 2021-06-17
        • 2022-07-22
        • 2021-07-20
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        相关资源
        最近更新 更多