【问题标题】:Put only existing subcollection in a list firebase(flutter)仅将现有子集合放入列表 firebase(flutter)
【发布时间】:2020-12-03 08:18:13
【问题描述】:

我一直在尝试将子集合的文档快照放入列表中。它的代码是这样的:-

getcoffeebuyerlist() async {
      DocumentSnapshot coffeebuyers;
      List<DocumentSnapshot> finalCoffeBuyerList = [];
      for (var i = 0; i < profiles.length; i++) {
        coffeebuyers = await Firestore.instance
            .collection('profileData')
            .document(profiles[i].uid)
            .collection('coffeeprices')
            .document(profiles[i].uid)
            .get();
        finalCoffeBuyerList.add(coffeebuyers);
      }
      return finalCoffeBuyerList;
    }

有两个配置文件,其中只有一个有子集合'coffeeprices'。

我的疑问是通过尝试获取子集合的快照,我是否会自动为不存在子集合的第二个配置文件创建一个空文档并将其放入finalCoffeBuyerList 列表中?

或者只有那些(在我的例子中)已经存在的子集合被添加到这个列表中?

【问题讨论】:

    标签: firebase flutter firebase-realtime-database


    【解决方案1】:

    您的代码将永远不会创建任何文档。如果文档不存在,get() 不会创建文档。您应该检查返回的DocumentSnapshot 以查看文档是否实际上是exists。像这样的:

    if (coffeebuyers.exists) {
      finalCoffeBuyerList.add(coffeebuyers);
    }
    

    创建文档的唯一方法是使用add() 或set()。

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 2019-05-27
      • 2020-08-28
      • 2021-03-05
      • 2021-10-30
      • 1970-01-01
      • 2023-02-14
      • 2014-11-30
      • 1970-01-01
      相关资源
      最近更新 更多