如何使用Flatter从Firebase中的子集合访问字段值?

如何使用Flatter从Firebase中的子集合访问字段值?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,对不起,我问错了。你们可以删除吗!对不起 在您调用以转换的方法中,您不会再次调用集合中的文档 List mapToList({DocumentSnapshot doc, List<DocumentSnapshot> docList}) { if (docList != null) { List<Store> storeList = []; docList.forEach((document) { //I WANT TO GE

对不起,我问错了。你们可以删除吗!对不起

在您调用以转换的方法中,您不会再次调用集合中的文档

List mapToList({DocumentSnapshot doc, List<DocumentSnapshot> docList}) {
    if (docList != null) {
      List<Store> storeList = [];
      docList.forEach((document) {
        //I WANT TO GET A FIELD IN THIS SUBCOLLECTION!!!
        String productName = document.reference.collection("products"); 
  //this does not give you the data but just a collection reference. 
}
}
列表映射列表({DocumentSnapshot doc,List docList}){
if(docList!=null){
List storeList=[];
docList.forEach((文件){
//我想在这个子集合中得到一个字段!!!
String productName=document.reference.collection(“产品”);
//这不会提供数据,而只是一个集合引用。
}
}
你需要再做一次这样的事

CollectionReference productsRef = document.reference.collection('products');
 List<DocumentSnapshot> productsnap = productsRef.getDocuments().documents;

productsnap.forEach((data){ data[StringConstant.productName] });

CollectionReference productsRef=document.reference.collection(“产品”);
List productsnap=productsRef.getDocuments().documents;
productsnap.forEach((数据){data[StringConstant.productName]});

如果我理解正确,您希望跳过“商店”集合,跳转到“产品”集合

List mapToList({DocumentSnapshot doc, List<DocumentSnapshot> docList}) {
    if (docList != null) {
      List<Store> storeList = [];
      docList.forEach((document) {
        //I WANT TO GET A FIELD IN THIS SUBCOLLECTION!!!
        String productName = document.reference.collection("products"); 
  //this does not give you the data but just a collection reference. 
}
}
您可以使用collectionGroup来完成

已更新

Firestore.instance.collectionGroup('products').snapshots().listen((data) {
      data.documentChanges.forEach((change) {
        if(change.document.data['category']=='banana')
        print('store name: ${change.document.reference.parent().parent().documentID}');
      });
    });

您好!谢谢您的帮助…但是在List productsSnap中,我遇到了以下错误:“getter”documents”没有为我正在使用的BLOC类定义,如果有帮助的话。请帮帮我。当然,我会尽力做到最好。我的坏消息getDocuments()返回一个未来。因此您可以等待QuerySnapshot productsnap=Wait productsRef.getDocuments();List=productsnap.documents;它看起来正在工作,它捕获了名称,但由于等待和异步,它没有构建Listview。我认为这与我的StreamBuilder有关。无论如何,谢谢。我会研究文档并尝试更好地理解。谢谢你!你能帮我将来构建这个函数映射列表吗???请!!!嘿,我的朋友!谢谢你的回答…我想要的是建立一个Firebase搜索系统,在这里我可以搜索产品,并在ListView.builder中建立拥有所选产品的商店。