【问题标题】:How to get the array field of the document in the firestore如何在firestore中获取文档的数组字段
【发布时间】:2021-06-29 18:03:38
【问题描述】:

我正在flutter中工作,并试图在firestore中获取文档的数组字段。但是这样做有几个问题:

  1. 无法将这个 id 字符串数组从 firestore 分配到 Flutter 中的 List 中
  2. 每次我尝试获取数组字段时,由于某种奇怪的原因,数组字段会从 firestore 中删除。

函数代码如下:

static List<String> getChatingTiles(String currentUserId) {
    CollectionReference users = firestore.collection("users");
    List<String> sessionIdList = <String>[];
    //trying to get the array field of the document that has an id of currentUserId
    users.doc(currentUserId).get().then((snapshot) => {
       //trying to loop through the array called sessions and adding every string element into the list
       for(String elem in snapshot.data()['sessions']) {
         sessionIdList.add(elem)
       }
    });
    print("list is here :");
    print(sessionIdList);
    return sessionIdList;
  }

【问题讨论】:

  • 在第 1 期中,sessionIdList 是否保持空白?您的 elem 迭代是否获得了非空值或者它可以为空。您还应该在 for 循环内打印以检查数据。对于第 2 期,这可能与其他一些与此处代码无关的 Firestore 更新逻辑有关。

标签: firebase flutter google-cloud-firestore nosql


【解决方案1】:

我建议您查看与您的非常相似的post,答案是:

“没有办法读取文档中的字段子集。您必须获取整个文档,然后使用点表示法或 FieldPath 来获取嵌套数据:”

【讨论】:

    【解决方案2】:

    你可以试试这个

    sessionid = snapshot.data['sessions']
    

    获取文档快照后返回

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 2019-11-27
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      相关资源
      最近更新 更多