Ahmed Nezhi
Ahmed Nezhi

Reputation: 318

Flutter Google maps not reloading after changing markers

I'm working with the google map widget, when I render the map for the first time with static markers it displays all the markers, but when I get the data from API and try to add new markers to the map, it won't display them.
below the code that i'm using :

class Map extends StatefulWidget {
  final Position initialPosition;
  final List<Marker> allSitesMarkers;
  final List<Site> placesSite;
  final List<String> mapMarkersCategories;
  const Map(this.initialPosition, this.allSitesMarkers, this.placesSite,
      this.mapMarkersCategories,
      {Key key})
      : super(key: key);
  @override
  State<StatefulWidget> createState() => MapState();
}

class MapState extends State<Map> {
  Completer<GoogleMapController> _controller = Completer();

  String currentCategoryFilter;
  bool filtred = false;
  Set<Marker> _markers = Set();

  List<Marker> markersToShow;
  @override
  void initState() {
    super.initState();
    refreshMarkers();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: (widget.placesSite.isNotEmpty)
          ? Stack(
              children: [
                GoogleMap(
                  initialCameraPosition: const CameraPosition(
                      target: LatLng(36.798, 10.1717), zoom: 15),
                  mapType: MapType.normal,
                  myLocationEnabled: true,
                  markers: _markers,
                  onMapCreated: (GoogleMapController controller) {
                    controller.setMapStyle(
                        '[ //... ]');

                    _controller.complete(controller);
                  },
                ),
                _chips()
              ],
            )
          : const NoListMap(),
    );
  }


  Widget _chips() {
    //..
  }

  filterSites(String category) {
    setState(() {
      _markers.clear();
      filtred = true;
      markersToShow = widget.allSitesMarkers
          .where((element) => element.infoWindow.title == category)
          .toList();
    });
    refreshMarkers();
  }

  refreshMarkers() {
    if (filtred) {
      setState(() {
        _markers.clear();
        _markers.addAll(markersToShow);
      });
    } else {
      setState(() {
        _markers.clear();
        _markers.addAll(widget.allSitesMarkers);
      });
    }
  }
}

I'm using:
Flutter 2.10.2
Dart 2.16.1
google_maps_flutter : 2.0.9

Upvotes: 2

Views: 5505

Answers (2)

Joel Sebastian
Joel Sebastian

Reputation: 1

This is what I Did to update the posistion

setState(() {
    mapController.animateCamera(CameraUpdate.newCameraPosition(
        CameraPosition(
            target: LatLng(widget.siteDetails?.location?.lat ?? 8.5555187,
                widget.siteDetails?.location?.long ?? 76.8836149),
            zoom: 11)));
    markers = <Marker>[
      Marker(
          markerId: const MarkerId('MarkId'),
          position: LatLng(widget.siteDetails?.location?.lat ?? 0,
              widget.siteDetails?.location?.long ?? 0),
          infoWindow: InfoWindow.noText)
    ];
  });

Upvotes: 0

Peter Koltai
Peter Koltai

Reputation: 9819

Your _markers is a Set, and in setState you use clear and addAll to empty it and to add new items to the set. But the value of _markers does not change when you do so, therefore it will not trigger a rebuild.

This happens because _markers holds a reference to the Set (like lists, objects etc.).

You need to assign a new value to _markers to trigger a rebuild, for example:

setState(() {
  _markers = markersToShow.toSet();
});

This will actually change the reference kept in _markers.

Upvotes: 4

Related Questions