【问题标题】:Filter Firestore stream results过滤 Firestore 流结果
【发布时间】:2020-04-17 15:00:27
【问题描述】:

我有一个位置列表,我想在地图上显示并且还想按类别进行过滤。我可以获取位置和地点标记,但不知道在选择下拉选项时如何过滤它们。

这是我目前所拥有的:

  getActiveLocations() {
    Stream<List<LocationModel>> locations =
        _locationController.getActiveLocations();

    locations.listen((location) async {
      print(location);
      for (var i = 0; i < location.length; i++) {
        String name = location[i].name;
        double lat = location[i].geometry['location']['lat'];
        double lng = location[i].geometry['location']['lng'];
        String icon = location[i].icon;

        final http.Response response = await http.get(icon);

        _markers.add(
          Marker(
            icon: BitmapDescriptor.fromBytes(response.bodyBytes),
            markerId: MarkerId(name),
            position: LatLng(lat, lng),
            onTap: () => _showModalSheet(location[i]),
          ),
        );
      }
    });
  }

  Stream<List<LocationModel>> getActiveLocations() {
    final CollectionReference ref = _db.collection('locations');

    try {
      return ref.snapshots().map(
            (QuerySnapshot list) => list.documents
                .map((DocumentSnapshot doc) => LocationModel.fromFirestore(doc))
                .toList(),
          );
    } catch (err) {
      print(err);
    }
    return null;
  }

我唯一能想到的就是在 onTap 上设置一个状态值,然后仅当位置包含该值时才添加到 _markers

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    我认为您可以直接从 Firestore 过滤信息。例如,如果有人在下拉菜单中选择了某些内容,您可以获得该选项并使用该选项来使用 Firestore 中的查询过滤结果,如提到的here

    这是他们在那里使用的示例代码:

    Firestore.instance.collection('collection')
      .where('field', isEqualTo: 'value')
      .orderBy('field')
      .snapshots()
      .listen((QuerySnapshot querySnapshot){
        querySnapshot.documents.forEach((document) => print(document));
      }
    );
    

    field 是您要查询的端口value 是从下拉列表中获得的选择。

    希望对你有帮助:)

    【讨论】:

    • 我最终创建了一个本地位置列表并根据类别对其进行过滤。
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 2015-05-17
    • 2017-08-27
    • 1970-01-01
    • 2012-07-08
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多