MultiMedia
MultiMedia

Reputation: 23

Why isn't Flutter Map updating with changes to coordinates in Consumer?

I'm trying to display a map in Flutter using flutter_map and update the coordinates using a Consumer. However, the map isn't updating when the coordinates change. If I use a Text widget instead of the map, the change works fine. What could be causing this issue and how can I fix it? Here's my code:

Consumer<DataManager>(
  builder: (context, manager, child) {
    return MapCoordinateWidget(
      coordinates: LatLng(
        manager.coordinates!.latitude,
        manager.coordinates!.longitude,
      ),
    );
  },
)

class MapCoordinateWidget extends StatelessWidget {
  MapCoordinateWidget({super.key, required this.coordinates});

  LatLng coordinates;

  @override
  Widget build(BuildContext context) {
    bool isLight = Theme.of(context).brightness == Brightness.light;
    String styleUrl = isLight
        ? "https://tiles.stadiamaps.com/tiles/alidade_smooth/{z}/{x}/{y}{r}.png"
        : "https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/{z}/{x}/{y}{r}.png";

    return ClipRRect(
      borderRadius: BorderRadius.circular(20),
      child: SizedBox(
        height: 180,
        child: FlutterMap(
          options: MapOptions(
            center: coordinates,
            zoom: 10,
            interactiveFlags: InteractiveFlag.none,
            keepAlive: true,
          ),
          children: [
            TileLayer(
              urlTemplate: "$styleUrl?api_key={api_key}",
              additionalOptions: const {"api_key": stadiaMapsApiKey},
              maxZoom: 20,
              maxNativeZoom: 20,
            )
          ],
        ),
      ),
    );
  }
}

I've already tried setting the keepAlive flag in MapOptions, but that didn't solve the problem.

Upvotes: 0

Views: 634

Answers (0)

Related Questions