Flutter - Removing markers from Google Maps Widget

582

I figured it out, the thing is, everything was working perfectly, but, the map wasnnt being rebuilt, because when i was yielding the state that was responsible for the map update, it wasnt being sent, because Equatable would stop the same state from being sent multiple times, so i simply removed Equatable from my MapState since i dont need it, and now everything is working perfectly!

Share:
582
RedZ
Author by

RedZ

Updated on December 29, 2022

Comments

  • RedZ
    RedZ over 1 year

    I'm building a flutter app and using BLoC for state management, where the user can select the current location of the camera, click "confirm" and a marker will be added in that place by the name of 'dep' (departure), then he will be able to add another marker by the name of 'dest' (destination). The user is also allowed to click "return" and revert back to the previous state which means id have to remove the last marker he used.

    Here is my google maps widget:

    GoogleMap(
      zoomControlsEnabled: false,
      zoomGesturesEnabled: true,
      myLocationEnabled: false,
      mapType: MapType.normal,
      initialCameraPosition: state.cameraPosition,
      markers: mapBloc.markers,
      onMapCreated: _onMapCreated,
      onCameraMove: _onCameraMove,
    ),
    

    Here is the code im using to remove markers (depending on the current state, markerID will be either "dep" or "dest"):

    void removeMarker(String markerID) {
        markers.remove(markers.firstWhere((Marker marker) => marker.markerId.value == markerID));
    }
    

    This screenshot is from the markers set after both "dep" and "dest" have been added to the map: enter image description here

    This screenshot is from the markers set after both "dep" and "dest" have been removed from the map: enter image description here

    The problem is, when i remove both markers, the second one (dest) is successfully removed from the map (after executing the map update function), but the first one (dep) is nnot removed, and it will only be updated when i try to 'add' a new 'dep' location, then the 'dep' marker will update its location to a new one.