I am using Google map in my Flutter web application , this is my widget :
class AddUserAddressMapBar extends ConsumerStatefulWidget {
const AddUserAddressMapBar(
{super.key,
required this.innerBoxIsScrolled,
this.userAddress,
this.onMapCreated,
this.markerUpdater});
final bool innerBoxIsScrolled;
final UserAddressModel? userAddress;
final void Function(GoogleMapController)? onMapCreated;
final MarkerUpdater? markerUpdater;
@override
ConsumerState createState() => _AddUserAddressMapBarState();
}
class _AddUserAddressMapBarState extends ConsumerState<AddUserAddressMapBar> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
if (widget.userAddress != null) {
final lat = widget.userAddress?.latitude!.toDouble();
final lng = widget.userAddress?.longitude!.toDouble();
ref.read(userAddressMapMarkerProvider.notifier).getMarker(lat!, lng!);
}
});
}
final Completer<GoogleMapController> _controllerCompleter = Completer();
void newLocForMarker(LatLng location) async {
if (_controllerCompleter.isCompleted) {
final controller = await _controllerCompleter.future;
try {
await controller
.animateCamera(CameraUpdate.newLatLngZoom(location, 14));
debugPrint("");
} catch (e) {
debugPrint(e.toString());
}
}
}
@override
Widget build(BuildContext context) {
final markers = ref.watch(userAddressMapMarkerProvider);
return SliverAppBar(
expandedHeight: MediaQuery.of(context).size.height - 600,
floating: true,
pinned: kIsWeb,
snap: true,
elevation: 0,
backgroundColor: Colors.transparent,
forceElevated: widget.innerBoxIsScrolled,
flexibleSpace: FlexibleSpaceBar(
stretchModes: const <StretchMode>[
StretchMode.zoomBackground,
StretchMode.blurBackground,
],
background: GoogleMap(
mapType: MapType.normal,
zoomGesturesEnabled: false,
zoomControlsEnabled: false,
scrollGesturesEnabled: false,
rotateGesturesEnabled: false,
myLocationButtonEnabled: false,
initialCameraPosition: CameraPosition(
target: LatLng(
(widget.userAddress?.latitude ?? 39.7392).toDouble(),
(widget.userAddress?.longitude ?? -104.9903).toDouble()),
zoom: 14.4746,
),
onMapCreated: (controller) async {
_controllerCompleter.complete(controller);
widget.markerUpdater?.call(context, newLocForMarker);
},
markers: markers,
onCameraMove: (position) {}),
),
);
}
}
I want to update marker on the map but for first time when I am coming to this widget and I am calling newLocForMarker , here controller
.animateCamera(CameraUpdate.newLatLngZoom(location, 14)); I get
Caught error: InvalidValueError: not a LatLng or LatLngLiteral with finite coordinates: in property lat: not a number
this is locations : 37.7768785, -122.4136676
But when I am back to previous screen and then I am back here, then when I update marker , the map will be updated and everything is fine. But I have issue when my widget loaded for first time .
Is someone faced with this issue?