Skip to content

fix: don't apply initialCameraFit on a zero-sized camera#2223

Merged
JaffaKetchup merged 2 commits into
fleaflet:masterfrom
nisenbeck:fix/zero-size-initial-camera-fit
Jul 2, 2026
Merged

fix: don't apply initialCameraFit on a zero-sized camera#2223
JaffaKetchup merged 2 commits into
fleaflet:masterfrom
nisenbeck:fix/zero-size-initial-camera-fit

Conversation

@nisenbeck

Copy link
Copy Markdown
Contributor

Fixes #2222 - see that issue for the full bug report, reproduction steps and root-cause analysis.

Fix

_applyInitialCameraFit could apply initialCameraFit against a zero-sized camera (constraints.maxWidth/maxHeight == 0) and mark it as permanently applied, because _parentConstraintsAreSet can return true in exactly that situation (its MediaQuery-based fallback is satisfied almost immediately, independently of whether this widget's own layout has actually settled).

Added an explicit, additional guard so the fit is only applied - and only latched as applied - once the constraints are genuinely non-zero in both dimensions:

if (!_initialCameraFitApplied &&
    widget.options.initialCameraFit != null &&
    _parentConstraintsAreSet(context, constraints) &&
    constraints.maxWidth > 0 &&
    constraints.maxHeight > 0) {
  _initialCameraFitApplied = true;
  _mapController.fitCamera(widget.options.initialCameraFit!);
}

I deliberately left _parentConstraintsAreSet itself untouched, rather than rewriting its heuristic - there's no history explaining why the MediaQuery fallback was added, so I didn't want to risk changing behaviour I can't fully verify. This fix instead directly prevents the harmful outcome (latching against a degenerate size) regardless of what _parentConstraintsAreSet decides. If the real constraints never arrive (the widget is genuinely given zero space forever), the fit simply never applies, which is a strictly better outcome than silently locking in a degenerate zoom.

Testing

Added test/map/zero_size_camera_fit_test.dart with two cases (there was no existing test coverage for initialCameraFit at all):

  • A real FlutterMap mounted inside a widget that reports Size.zero for one frame before revealing its real size: asserts the resulting camera.zoom matches a direct fit against the real size (previously it settled on 0.0, the degenerate zoomed-out result).
  • The same fit with the real size available from the very first frame (no race), to confirm the already-working case is unaffected by this change.

Full existing test suite passes locally with no regressions.

Manually verified on a physical Android device using the reproduction linked in the issue, pointed at this branch via a dependency_overrides: path: entry.

AI disclosure

Per CONTRIBUTING.md: I used AI assistance (Claude Code) to help diagnose the root cause and draft this fix and its tests. I've reviewed the change, verified it against the project's lint rules and test suite, and manually confirmed the fix resolves the issue on a physical device.

_applyInitialCameraFit could apply initialCameraFit against a
zero-sized camera and mark it as permanently applied, because
_parentConstraintsAreSet can return true in exactly that situation
(its MediaQuery-based fallback is satisfied almost immediately,
independently of whether this widget's own layout has actually
settled). This produced a permanent, degenerate (fully zoomed-out)
camera position with no way to recover short of user interaction.

Added an explicit guard so the fit is only applied - and only
latched as applied - once the constraints are genuinely non-zero in
both dimensions.
@JaffaKetchup JaffaKetchup changed the title fix: don't latch initialCameraFit as applied against a zero-sized camera fix: don't apply initialCameraFit on a zero-sized camera Jul 2, 2026

@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.

LGTM! Thanks again :)

@JaffaKetchup JaffaKetchup merged commit 63732f3 into fleaflet:master Jul 2, 2026
10 of 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.

initialCameraFit can silently apply against a zero-sized parent and never retry with the correct size

2 participants