【问题标题】:How can I check the size of a document in firestore is? [duplicate]如何检查firestore中文档的大小? [复制]
【发布时间】:2021-10-20 08:38:23
【问题描述】:

因为 firestore 中的文档有 1MiB 的限制,所以我想查看我的文档以检查它们有多大。有没有一种简单的方法可以找出文档的大小?

【问题讨论】:

    标签: database firebase flutter dart google-cloud-firestore


    【解决方案1】:

    This page 解释了每种数据类型的大小以及如何计算存储大小的说明,因此您可以通过编写函数来实际计算它。不幸的是,我无法为 Dart 找到这样做的包。有一个名为 firestore-size 的 npm 包,它可能有助于在 Dart 中编写相同的函数。

    基本上,您只需要遍历文档中的所有字段并继续添加它们各自的大小,例如,如果值为 hello 添加 6,如文档所述,对于字符串,大小将为“UTF 数-8 编码字节 + 1',对于 null 加 1 等等..

    【讨论】:

      【解决方案2】:

      所以你要检查手机上文件的大小:

      功能:

      getFileSize(String filepath, int decimals) async {
          var file = File(filepath);
          int bytes = await file.length();
          if (bytes <= 0) return "0 B";
          const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
          var i = (log(bytes) / log(1024)).floor();
          return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + ' ' + suffixes[i];  
      }
      

      使用方法:

      print(getFileSize('file-path-will-be-here', 1));
      

      结果如何:

      97.5 KB
      

      【讨论】:

        猜你喜欢
        • 2018-08-15
        • 2019-10-13
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        • 2021-09-10
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        相关资源
        最近更新 更多