Skip to content

[BUG] Repositioning the map through MapController in onMapReady prevents initial tiles loading #1507

@MatyasK

Description

@MatyasK

What is the bug?

After migrating to v4 the tiles doesn't load. if you move the map around manually the tiles start to load.

How can we reproduce it?

class CcaMapWidget extends StatelessWidget with CcaLogger {
  CcaMapWidget({
    required this.tripLegs,
    MapController? mapController,
    PopupController? popupLayerController,
    super.key,
  })  : _mapController = mapController ?? MapController(),
        _popupLayerController = popupLayerController ?? PopupController();

  final MapController _mapController;
  final PopupController _popupLayerController;
  final List<TripDetailLeg> tripLegs;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => CcaMapCubit()..screenLoaded(tripLegs),
      child: BlocBuilder<CcaMapCubit, CcaMapState>(
        builder: (context, state) {
          if (state.status == MapStatus.loading) {
            return const CcaLoadingIndicator();
          }

          return FlutterMap(
            mapController: _mapController,
            options: MapOptions(
              maxZoom: 13,
              onMapReady: () {
                final markerBounds = LatLngBounds.fromPoints(state.points);

                _mapController.fitBounds(
                  markerBounds,
                  options: const FitBoundsOptions(padding: EdgeInsets.all(70)),
                );
              },
            ),
            children: [
              TileLayer(
                urlTemplate: 'https://api.tomtom.com/map/1/tile/basic/main/'
                    '{z}/{x}/{y}.png?key={apiKey}',
                additionalOptions: {
                  'apiKey': getIt<UserRepository>().tomtomApiKey,
                },
                errorTileCallback: (image, error, stackTrace) {
                  logger.warning('Error: $error, $stackTrace');
                },
              ),
              PolylineLayer(
                polylines: [
                  Polyline(
                    strokeWidth: 3,
                    points: state.points,
                    color: kcPrimary,
                  ),
                ],
              ),
              PopupMarkerLayerWidget(
                options: PopupMarkerLayerOptions(
                  popupController: _popupLayerController,
                  markers: state.waypoints
                      .map((e) => WaypointMarker(wayPoint: e))
                      .toList(),
                  markerRotateAlignment:
                      PopupMarkerLayerOptions.rotationAlignmentFor(
                    AnchorAlign.top,
                  ),
                  popupBuilder: (BuildContext context, Marker marker) {
                    if (marker is WaypointMarker) {
                      return WaypointMarkerPopup(wayPoint: marker.wayPoint);
                    }

                    return const Card(child: Text('Not a waypoint'));
                  },
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}

Do you have a potential solution?

No response

Platforms

Android

Severity

Obtrusive: Prevents normal functioning but causes no errors in the console

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue reports broken functionality or another error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions