Skip to content

fix: prevent LateInitializationError when viewer state modified before layout - #2294

Merged
JaffaKetchup merged 7 commits into
fleaflet:masterfrom
codeOfJannik:fix/controller-without-viewer
Dec 1, 2026
Merged

JaffaKetchup merged 7 commits into
fleaflet:masterfrom
codeOfJannik:fix/controller-without-viewer

Conversation

@codeOfJannik

Copy link
Copy Markdown
Contributor

Fixes #2293

MapControllerImpl._interactiveViewerState is late and only assigned once the map's MapInteractiveViewer is created, which happens during layout. onMapReady fires after the first frame regardless, so a FlutterMap that is built but not laid out (e.g. on a route covered by another route from the first frame) throws LateInitializationError on any controller move (_emitMapEvent) and on any interactionOptions change (options=). See the issue for the details and a minimal app.

Changes

  • _interactiveViewerState is now nullable.
  • options= skips updateGestures while there is no viewer. The viewer builds its gestures from the controller's current options in didChangeDependencies, so it starts out with the latest options.
  • _emitMapEvent skips interruptAnimatedMovement while there is no viewer. There is no viewer-driven gesture animation to interrupt.

Behaviour for maps that have been laid out is unchanged.

Tests

New test/map/unlaid_out_map_test.dart, with the map on a route that an opaque route covers from the first frame:

  • moving the camera from onMapReady does not throw, and the move is kept once the map is shown;
  • changing the interaction options before the map is laid out does not throw, and the new options apply once it is (verified by dragging the map).

Both tests fail on master with LateInitializationError: Field '_interactiveViewerState@…' has not been initialized. and pass with this change. The full suite passes (142 tests) and dart analyze is clean.

@JaffaKetchup JaffaKetchup left a comment

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.

See #2293 (comment).

I think a better fix would be to fix onMapReady not to be called to early, unless that is difficult for some reason.

@JaffaKetchup JaffaKetchup left a comment

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.

Although I'm cautious about changing this particular part of the code since it's been the culprit for many bugs before, I think this change is good to go.

Thanks!

@JaffaKetchup JaffaKetchup changed the title fix: allow using MapController before the map has been laid out fix: prevent LateInitializationError when controller used before layout Dec 1, 2026
@JaffaKetchup JaffaKetchup changed the title fix: prevent LateInitializationError when controller used before layout fix: prevent LateInitializationError when viewer state modified before layout Dec 1, 2026
@JaffaKetchup
JaffaKetchup merged commit 7502403 into fleaflet:master Dec 1, 2026
11 checks passed
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 thrown when MapController used before the map layout, even after onMapReady

2 participants