【发布时间】:2021-06-29 18:03:38
【问题描述】:
我正在flutter中工作,并试图在firestore中获取文档的数组字段。但是这样做有几个问题:
- 无法将这个 id 字符串数组从 firestore 分配到 Flutter 中的 List 中
- 每次我尝试获取数组字段时,由于某种奇怪的原因,数组字段会从 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