'Flutter and widget updates
I have the following state of a statefull widget
class _ParkingMapState extends State<ParkingMap> {
@override
Widget build(BuildContext context) {
final position = Provider.of<Position?>(context);
CameraPosition cameraPosition = (position != null) ? CameraPosition(
target: LatLng(position.latitude, position.longitude), zoom: 14.0) : const CameraPosition(
target: LatLng(34.345, 34.345), zoom: 14.0);
return GoogleMap(
initialCameraPosition: cameraPosition,
);
}
}
position is a Future of Position from the geolocator library that is resolved through a future provider. I want to be able to load the map with a random position if for some reason position wasn't able to resolve and with the user's current position if the Future is resolved. My problem is that maps widget in my case get's renderred normally in the random position (since initially position is null) and it doesn't update.
Updating my return value to
return (position != null) ? GoogleMap(...) : const Center(child: CircularProgressIndicator());
I get the desired result, and map renders the current position in the map once position has been resolved. Could you explain the mechanics a bit so that I can understand why this is hapenning?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
