Skip to content

fix: only call onMapReady once the map has been laid out - #2310

Open
codeOfJannik wants to merge 1 commit into
fleaflet:masterfrom
codeOfJannik:fix/on-map-ready-after-layout
Open

codeOfJannik wants to merge 1 commit into
fleaflet:masterfrom
codeOfJannik:fix/on-map-ready-after-layout

Conversation

@codeOfJannik

Copy link
Copy Markdown

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:

  • 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's uncovered.
  • A map disposed before it's ever laid out never gets it.

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.

`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]>
Comment thread lib/src/map/widget.dart
Comment on lines +99 to 100
_scheduleMapReady();
_updateAndEmitSizeIfConstraintsChanged(constraints);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 initialCameraFit set, a move in onMapReady is overwritten by the fit (this happens on master too). With your order, the move wins.
  • The first MapEventNonRotatedSizeChange is emitted after the move but carries the camera from before it, so onMapEvent listeners 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.

Comment thread lib/src/map/widget.dart
Comment on lines +148 to +150
/// Called before [_updateAndEmitSizeIfConstraintsChanged], so `onMapReady`
/// keeps running before the first size change event and the initial camera
/// fit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JaffaKetchup

Copy link
Copy Markdown
Member

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 _scheduleMapReady in #2310 (comment), there's one frame of the map being in its initial position, because of course the callback is fired a frame late.

Thanks for opening this, and I'll consider whether its best to merge just #2294 or both.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LateInitializationError on _interactiveViewerState when a MapController is used after onMapReady but before the map has been laid out

2 participants