Skip to content

Stale camera in post-frame nonRotatedSizeChange discards tiles when a debounced tileUpdateTransformer keeps only the last event #2270

Closed as duplicate

Description

@WudgeJalk

Version: 8.3.2

Repro: register controller.fitCamera(...) in a post-frame callback before the first layout of FlutterMap (e.g. from a parent's initState), and give the TileLayer a tileUpdateTransformer that debounces and delivers only the last event.

Expected: tiles for the fitted camera load.
Actual: the fitted view stays without its tiles until the next gesture.

Cause: _updateAndEmitSizeIfConstraintsChanged (widget.dart) captures newMapCamera at layout, then emits MapEventNonRotatedSizeChange with it in a post-frame scheduled after the fit's. That event carries the pre-fit camera, arrives last, so the debounced TileLayer loads/prunes for the stale camera and discards the fitted tiles.

Fix (one line): emit _mapController.camera read at post-frame time instead of the captured newMapCamera.

We have this patched locally and can open a PR if you want it.

Activity

  1. JaffaKetchup commented on Oct 7, 2026

    @JaffaKetchup
    Member

    Hey, thanks for opening this.

    I believe you're running into a documented issue at https://docs.fleaflet.dev/usage/programmatic-interaction#the-controller-is-not-safe-to-use-until-attached, specifically the warning box there.

    That makes this a duplicate of #1507 I think.

    If you've found a usecase in which you need to use the map like this, then we can look into it, please do ping me. But there should be an alternative using the map options, the intended route: https://docs.fleaflet.dev/usage/options#initial-positioning. Please let me know if that doesn't work for you.

    Because I marked that last issue as won't fix with some agreement from others, and I still don't want to change this part of the code unnecessarily, I'm going to mark this as won't fix/duplicate as well.

    Again, if I've missed anything or made any wrong assumptions, please do ping me :). And we're always greatful for more issues and PR contributions in future!

  2. WudgeJalk commented on Oct 9, 2026

    @WudgeJalk
    Author

    Thanks for the look. I think this one isn't the attach-before-build case, so a quick ping as you suggested.

    The controller is used after the first build in both repros: in a post-frame callback, and — the documented route — in onMapReady. onMapReady is scheduled in initState (widget.dart L59-60) and the size-change event is scheduled during layout, so onMapReady runs first and the stale event overtakes any move/fitCamera made there. With a debounced tileUpdateTransformer the last event wins and the tiles of the pre-move camera are loaded instead. That's also what the last comment on #1507 (#1507 (comment), move() in onMapReady) describes.

    Checked on a clean v8.3.2 checkout:

    test v8.3.2 + one-line fix
    move in onMapReady, debounced tile layer fails (only z3 tiles load) passes
    move in an early post-frame callback (PR test) fails passes
    existing suite (119 tests) — passes

    The fix only changes which camera the already-scheduled event carries; the event order and layout code stay as they are. The onMapReady test is now on the PR branch too — would you reopen #2271?

  3. added a commit that references this issue on Oct 9, 2026
    1836a06
  4. JaffaKetchup commented on Oct 10, 2026

    @JaffaKetchup
    Member

    Please can you tell me why you need to use the controller in this way, rather than setting the camera fit through MapOptions?

    Please also add an MRE, minimally reproducible example.

    and — the documented route — in onMapReady.

    This is explicitly mentioned in my link above to NOT be allowed.

  5. WudgeJalk commented on Oct 11, 2026

    @WudgeJalk
    Author

    You're right, onMapReady is explicitly excluded in the docs — my mistake.

    Why the controller: the map is shown at once on a reference point, a trace loads asynchronously and the camera is fitted on it; the same map is re-fitted when the user picks another trace. initialCameraFit only applies once, at creation, so short of recreating the map with a new key, MapOptions doesn't cover this.

    The bug needs no discouraged pattern, though. The size-change event is emitted from a post-frame callback scheduled during layout, with the camera read at layout. If an earlier post-frame callback of that frame moves the camera (typically "resize, then fit once laid out"), the event arrives last with the old camera, and the fitted view ends up with tiles that never start loading until the next map event. With the default transformer, Epsg3857 hides it only because DiscreteTileRange.contains wraps across zoom levels (what #2277 changes); Epsg4326 shows it as is, and any CRS does with a debouncing transformer.

    MRE below, no onMapReady, no controller use before the first build: on v8.3.2 (Windows, pub package, no override), tap the button and the fitted view stays grey until the next map event. As a widget test: 0 of 9 visible tiles start loading, 9 of 9 with #2271.

    If you'd still rather leave it, fine — we keep the line in our fork.

    MRE (main.dart)
    import 'package:flutter/material.dart';
    import 'package:flutter_map/flutter_map.dart';
    import 'package:latlong2/latlong.dart';
    
    void main() => runApp(const MaterialApp(home: Mre()));
    
    class Mre extends StatefulWidget {
      const Mre({super.key});
      @override
      State<Mre> createState() => _MreState();
    }
    
    class _MreState extends State<Mre> {
      final _controller = MapController();
      bool _panel = true;
    
      void _togglePanelAndFit() {
        setState(() => _panel = !_panel);
        // Fit once the map has been laid out at its new size.
        WidgetsBinding.instance.addPostFrameCallback(
          (_) => _controller.fitCamera(
            CameraFit.bounds(
              bounds: LatLngBounds(const LatLng(46, 6), const LatLng(46.3, 6.5)),
            ),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) => Scaffold(
            floatingActionButton: FloatingActionButton(
              onPressed: _togglePanelAndFit,
              child: const Icon(Icons.fit_screen),
            ),
            body: Row(
              children: [
                if (_panel) const SizedBox(width: 300),
                Expanded(
                  child: FlutterMap(
                    mapController: _controller,
                    options: const MapOptions(
                      crs: Epsg4326(),
                      initialCenter: LatLng(0, 0),
                      initialZoom: 2,
                    ),
                    children: [
                      TileLayer(
                        wmsOptions: WMSTileLayerOptions(
                          crs: const Epsg4326(),
                          baseUrl: 'https://ows.mundialis.de/services/service?',
                          layers: const ['TOPO-OSM-WMS'],
                        ),
                        userAgentPackageName: 'mre.issue2270',
                      ),
                    ],
                  ),
                ),
              ],
            ),
          );
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions