You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stale camera in post-frame nonRotatedSizeChange discards tiles when a debounced tileUpdateTransformer keeps only the last event #2270
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.
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!
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?
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';
voidmain() =>runApp(constMaterialApp(home:Mre()));
classMreextendsStatefulWidget {
constMre({super.key});
@overrideState<Mre> createState() =>_MreState();
}
class_MreStateextendsState<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(constLatLng(46, 6), constLatLng(46.3, 6.5)),
),
),
);
}
@overrideWidgetbuild(BuildContext context) =>Scaffold(
floatingActionButton:FloatingActionButton(
onPressed: _togglePanelAndFit,
child:constIcon(Icons.fit_screen),
),
body:Row(
children: [
if (_panel) constSizedBox(width:300),
Expanded(
child:FlutterMap(
mapController: _controller,
options:constMapOptions(
crs:Epsg4326(),
initialCenter:LatLng(0, 0),
initialZoom:2,
),
children: [
TileLayer(
wmsOptions:WMSTileLayerOptions(
crs:constEpsg4326(),
baseUrl:'https://ows.mundialis.de/services/service?',
layers:const ['TOPO-OSM-WMS'],
),
userAgentPackageName:'mre.issue2270',
),
],
),
),
],
),
);
}
Activity
JaffaKetchup commentedon Oct 7, 2026
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!
MapControllerinonMapReadyprevents initial tiles loading #1507WudgeJalk commentedon Oct 9, 2026
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.onMapReadyis scheduled ininitState(widget.dartL59-60) and the size-change event is scheduled during layout, soonMapReadyruns first and the stale event overtakes anymove/fitCameramade there. With a debouncedtileUpdateTransformerthe 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()inonMapReady) describes.Checked on a clean v8.3.2 checkout:
moveinonMapReady, debounced tile layermovein an early post-frame callback (PR test)The fix only changes which camera the already-scheduled event carries; the event order and layout code stay as they are. The
onMapReadytest is now on the PR branch too — would you reopen #2271?fix: emit current camera in post-frame `MapEventNonRotatedSizeChange`
JaffaKetchup commentedon Oct 10, 2026
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.
This is explicitly mentioned in my link above to NOT be allowed.
WudgeJalk commentedon Oct 11, 2026
You're right,
onMapReadyis 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.
initialCameraFitonly applies once, at creation, so short of recreating the map with a new key,MapOptionsdoesn'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.containswraps 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)