【问题标题】:Flutter: how to count items in a Firebase field?Flutter:如何计算 Firebase 字段中的项目?
【发布时间】:2021-08-17 13:11:11
【问题描述】:

如何将我在此文档字段(成分)中的项目数量设为整数? (在这种情况下为 5)

我试过了:

void countDocuments() async {
  QuerySnapshot _myDoc =
  await Firestore.instance.collection('product').getDocuments();
  List<DocumentSnapshot> _myDocCount = _myDoc.documents;
  print(_myDocCount.length); // Count of Documents in Collection
}

但这似乎不适用于我的情况:

谢谢!

【问题讨论】:

  • 您需要将 Map 映射到一个列表并计算 Map 有多少键/值。到目前为止你有没有尝试过?您需要展示进度。
  • 我试过stackoverflow.com/questions/54456665/…,但在我的情况下,'firestore' 关键字似乎会造成麻烦
  • firestore 关键字?你的意思是?你能显示你的代码吗?你下载了库并导入了吗?
  • @MarianoZorrilla 我编辑了这个问题。请检查
  • 请重新检查您的代码。您在方法之上定义了一个变量,然后您尝试调用一个不存在的类,因为未定义“Firebase”......它被称为“FirebaseFirestore”

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

请按照官方文档连接 Firebase Firestore。

https://firebase.flutter.dev/docs/firestore/usage

使用 FirebaseFirestore.instance 代替 Firestore.instance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2022-11-15
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多