I have the following class for a google map in Flutter.
class ExchangeHousesMap extends StatefulWidget {
final List<Marker> markers;
final double initialLatitude;
final double initialLongitude;
const ExchangeHousesMap({
Key? key,
required this.initialLatitude,
required this.initialLongitude,
required this.markers,
}) : super(key: key);
@override
State<ExchangeHousesMap> createState() => ExchangeHousesMapState();
}
class ExchangeHousesMapState extends State<ExchangeHousesMap> {
late final CameraPosition _initialCameraPosition;
late final Set<Marker> _markers = {};
final Completer<GoogleMapController> _controller = Completer();
@override
void initState() {
super.initState();
_initialCameraPosition = CameraPosition(
target: LatLng(widget.initialLatitude, widget.initialLongitude),
zoom: 12,
);
}
@override
Widget build(BuildContext context) {
return GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _initialCameraPosition,
markers: _markers,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
setState(
() {
_markers.addAll(widget.markers);
_markers.add(
Marker(
markerId: const MarkerId('user-marker'),
position: LatLng(widget.initialLatitude, widget.initialLongitude),
),
);
},
);
},
);
}
}
I am rendering this google map as a child of a parent widget. I want to add a new marker to this map upon tapping on a button in the parent widget.
I can not figure out how to do that. How to get a marker added to this map upon tapping that button in the parent widget?
Can someone please help?