首页 > 解决方案 > 如何使用 Dart 将文档字段存储在列表中

问题描述

我正在尝试从文档中检索数组字段并将其存储在列表对象中。

这是我到目前为止所尝试的:

   List<String> list;

Firestore.instance.collection("ingredients").document("Cutlet").get()
.then((snapshot) => list = snapshot.data["ingredients"]);

debugPrint("list is" + list.toString());
return list;

debugPrint 返回 null。

这是我的firestore的快照。我正在尝试检索成分数组并将其存储在列表中。

在此处输入图像描述

标签: dartfluttergoogle-cloud-firestore

解决方案


尝试这个

 DocumentSnapshot doc= await Firestore.instance.collection('ingredients').document('cutlet').get();
  if(doc.exists) {
    var list = doc['ingredients'].map<dynamic>((item) {
      return item;
    }).toList();
  }

推荐阅读