使用Flutter获取Firebase中的子集合字段?

时间:2019-11-15 15:52:10

标签: firebase flutter dart google-cloud-firestore

我想构建一个从Subcolletion获取一个字段的搜索系统,并构建一个带有其父字段的Lis​​t。例如,我想在子产品集合中搜索一个名为“香蕉”的产品,并从父集合中获取所有商店,STORES的子集合“产品”中有“香蕉”。

如果您对其他代码有更好的解决方案,我也希望。如果我必须重建,我不在乎。我想用我的代码或其他完全不同的代码来解决此问题,因为它可以正常工作。谢谢!!!

这是我的打印屏幕:

enter image description here

enter image description here

这是我的Firebase功能:

Stream<QuerySnapshot> searchList() {
    return _firestore
        .collection('stores')
        .snapshots();
  }

这是我的列表函数:

  //Convert map to goal list
  List mapToList({DocumentSnapshot doc, List<DocumentSnapshot> docList})  {
    if (docList != null) {
      List<Store> storeList = [];
      docList.forEach((document) async {

        String productName; //HOW CAN I GET THE FIELD NAME FROM PRODUCTS SUBCOLLETION?

        String name = document.data[StringConstant.nameField];
        num score = document.data[StringConstant.scoreField];
        String delivery = document.data[StringConstant.deliveryField];
        String photo = document.data[StringConstant.photoField];
        String description = document.data[StringConstant.descriptionField];
        String open = document.data[StringConstant.openField];
        String close = document.data[StringConstant.closeField];
        GeoPoint geoPoint = document.data[StringConstant.positionField]
        [StringConstant.geopointField];

        bool isOpen = document.data[StringConstant.isOpenField];
        final currentHour = DateTime.now();
        final openHour = DateTime.parse(open).hour;
        final closeHour = DateTime.parse(close).hour;
        int openMin = DateTime.parse(open).minute;
        int closeMin = DateTime.parse(close).minute;
        if (openHour <= currentHour.hour && currentHour.hour <= closeHour) {
          isOpen = true;
          if ((currentHour.hour == openHour && currentHour.minute < openMin) ||
              (currentHour.hour == closeHour &&
                  currentHour.minute > closeMin)) {
            isOpen = false;
          }
        } else {
          isOpen = false;
        }
        final double meter = distance(
          LatLng(latitude, longitude),
          LatLng(geoPoint.latitude, geoPoint.longitude),
        );

        if (meter <= range && isOpen == true) {
          Store otherStore = Store(name, photo, score.toDouble(), delivery,
              meter, description, productName);
          await storeList.add(otherStore);
        }
      });
      return storeList;
    } else {
      return null;
    }
  }

0 个答案:

没有答案