Adding markers dynamically to flutter google map Adding markers dynamically to flutter google map flutter flutter

Adding markers dynamically to flutter google map


If there's no specific reason for you to use Map data structure, here's what I've done in the past.

I have a Set of Marker in my State

Set<Marker> markers = Set();

Give it to the map widget. markers: markers

GoogleMap(  onMapCreated: _onMapCreated,  myLocationEnabled: true,  initialCameraPosition:    CameraPosition(target: LatLng(0.0, 0.0)),  markers: markers,)

And then adding the Marker, which I'm building with search result and which you'll be building with your user's location, to Set of Marker in setState method.

// Create a new markerMarker resultMarker = Marker(  markerId: MarkerId(responseResult.name),  infoWindow: InfoWindow(  title: "${responseResult.name}",  snippet: "${responseResult.types?.first}"),  position: LatLng(responseResult.geometry.location.lat,  responseResult.geometry.location.lng),);// Add it to Setmarkers.add(resultMarker);

Edit: I've just noticed you're using GoogleMap widget in your initState, you need to move it to the build method if you want to rebuild it everytime with the new state values.