1

So i am using the googlemaps package and when a user zooms in/out i need to show/hide various markers based on distance. the markers data comes from a json future. I can see it zoom in/out, but i cannot get it to loop through my amenities future. i know i am probably using the wrong thing(FutureBuilder) what should i be using instead? thanks

class _AlreadyHereState extends State<AlreadyHere>
    with SingleTickerProviderStateMixin {
 late Future<Amenities> amenities;

  @override
  void initState() {
    super.initState();
    amenities = AmenitiesData.getAmenities();
  }


  onCameraMove(CameraPosition position) {
    double distance;
    if (position.zoom >= 10.1) {
      // Create Markers From Amenities
      print("Add Marker - $position");
      FutureBuilder(
          future: amenities,
          builder: (BuildContext context, snapshot) {
            if (snapshot.hasData) {
              snapshot.data!.amenities.forEach((el) {
                distance = calculateDistance(position.target.latitude,
                    position.target.latitude, el.latitude, el.longitude);
                if (distance <= 5) {
                  //Create Marker
                } else {
                  //  if marker exists remove marker
                }
              });
            }
            throw ('error');
          });
    } else if (position.zoom <= 10.0) {
      print("Remove Marker - $position");
      //Remove Markers
    }
  }
jamie1231
  • 29
  • 4

1 Answers1

0

here you definitely need to use some form of state management. then there is no need to use FutureBuilder. it can be a Provider or a BLoC. moreover, then all async logic of the calculateDistance(..) type will go there and you will just need to update the state with ready-made markers.

Vladyslav Ulianytskyi
  • 1,401
  • 22
  • 22