Repository navigation
fix: only call onMapReady once the map has been laid out - #2310
codeOfJannik wants to merge 1 commit into
Conversation
`onMapReady` was scheduled from `FlutterMap`'s `initState`, so it fired after the first frame even for a map that was built but not laid out, for example on a route that another opaque route covers from the first frame. Such a map has no `MapInteractiveViewer` yet, so using the controller from `onMapReady` threw a `LateInitializationError`. `onMapReady` is now scheduled from the map's first layout. A map laid out in its first frame gets it at the same point as before, still ahead of the first size change event and the initial camera fit. A covered map gets it once it is uncovered, and never if it is disposed before that. Fixes fleaflet#2293. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| _scheduleMapReady(); | ||
| _updateAndEmitSizeIfConstraintsChanged(constraints); |
There was a problem hiding this comment.
| _scheduleMapReady(); | |
| _updateAndEmitSizeIfConstraintsChanged(constraints); | |
| _updateAndEmitSizeIfConstraintsChanged(constraints); | |
| _scheduleMapReady(); |
I found that this order seems to work better. Otherwise, events happen before the map knows its own size - so while, for example, a MapController.move works, tiles don't get loaded because there's no size. In this order, tiles are loaded and the callback is called as soon as the map knows its size.
I note that you explain in the doc comment on _scheduleMapReady that you put them in the current order deliberately - is there a situation you can think of where you would want onMapReady to be called before the initial camera fit?
There was a problem hiding this comment.
Good point, and the suggested order makes sense to me. There's no case I can think of where onMapReady should run before the initial camera fit. I only kept that order because it's what master does today: onMapReady is scheduled in initState, so it already runs before the first size change event and the initial fit, and I wanted maps that are laid out immediately to keep the same behaviour.
Testing it, though, the current order has two real downsides:
- With
initialCameraFitset, a move inonMapReadyis overwritten by the fit (this happens onmastertoo). With your order, the move wins. - The first
MapEventNonRotatedSizeChangeis emitted after the move but carries the camera from before it, soonMapEventlisteners and the tile layer briefly see a stale camera.
The size itself is already known in both orders (it's set during layout, before either callback runs), so I think the stale event is what you saw with the tiles.
One thing to keep in mind with the swap: onMapReady would then run after the first size change event and the initial fit for every map, not just covered ones. Code that starts listening to mapEventStream in onMapReady would no longer see that first size event. I'd expect that to be rare, but it may be worth a line in the changelog.
| /// Called before [_updateAndEmitSizeIfConstraintsChanged], so `onMapReady` | ||
| /// keeps running before the first size change event and the initial camera | ||
| /// fit. |
There was a problem hiding this comment.
|
I've read through the reasoning in #2293 (comment), and actually I do agree now that #2294 might be a good idea, or using both PRs. I've also noticed that with this approach, for either location of Thanks for opening this, and I'll consider whether its best to merge just #2294 or both. |
Fixes #2293. An alternative to #2294, as suggested in the review there. Only one of the two should be merged.
onMapReady was scheduled from FlutterMap.initState, so it fired after the first frame even for a map that was built but not laid out (for example on a route covered by an opaque route from the first frame). Such a map has no MapInteractiveViewer yet, so using the controller from onMapReady threw a LateInitializationError.
onMapReady is now scheduled from the map's first layout:
I also updated the onMapReady docs, which said it fires after initState, and pointed them to the initial* options for initial positioning. Tests are in test/map/unlaid_out_map_test.dart. #2294 adds a file with the same name, so whichever PR is closed resolves that.