0

I have a list of locations that I want to display on a map and also want to filter by a category. I can get the locations and place markers, but don't know how to filter them when an option from a dropdown is selected for example.

This is what I have so far:

  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;
  }

Only thing I can think of is setting a state value onTap and then add to _markers only if the location contains the value.

William
  • 884
  • 2
  • 12
  • 23

1 Answers1

0

I think that you can filter the info from Firestore directly. For example, if someone selects something in the dropdown menu, you can get the option and use that selection to filter the results using a query in Firestore like mentioned here.

Here's the example code they use there:

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

Where field is over which porperty you're going to query value is the selection that was got from the dropdown.

I hope this can help you :)

Puteri
  • 3,348
  • 4
  • 12
  • 27