Skip to content

fix: prevent NaN fling direction when tracked pointer offsets are zero#2220

Merged
JaffaKetchup merged 1 commit into
fleaflet:masterfrom
nisenbeck:fix/fling-nan-division-by-zero
Jul 1, 2026
Merged

fix: prevent NaN fling direction when tracked pointer offsets are zero#2220
JaffaKetchup merged 1 commit into
fleaflet:masterfrom
nisenbeck:fix/fling-nan-division-by-zero

Conversation

@nisenbeck

@nisenbeck nisenbeck commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

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

Fix

MapInteractiveViewerState._handleScaleEnd could divide flingOffset by a
zero flingDistance (0.0 / 0.0 = NaN) when both the final tracked pointer
segment and the overall drag offset happened to have zero length, even
though the gesture's reported velocity was above the fling threshold.

Extracted the direction calculation into a small, pure, unit-testable
flingDirection helper, and added a final fallback to the (already
guaranteed non-zero) velocity direction instead of dividing by zero:

if (finalSegmentDistance > 0) return finalSegment / finalSegmentDistance;
if (flingDistance > 0) return flingOffset / flingDistance;
return velocityDirection;

This preserves the existing behaviour (prefer the final tracked segment,
then the overall drag direction) and only changes behaviour for the
previously-undefined zero/zero edge case, where it now falls back to the
same velocity-based direction that flutter_map used prior to #2158 (8.2.2
and earlier).

velocityDirection is guaranteed finite: magnitude is already checked to
be >= _kMinFlingVelocity before this is called, and Flutter's own
VelocityTracker / LeastSquaresSolver never return NaN - they fall back
to Offset.zero / null on ill-conditioned samples - so this can never
itself be a source of NaN.

Testing

  • Added test/gestures/map_interactive_viewer_test.dart covering all three
    branches of flingDirection, including a regression test for the
    zero/zero case.
  • Full existing test suite passes locally with no regressions.
  • Manually verified on a physical Android device: confirmed the crash on
    8.3.0/8.3.1, confirmed it does not occur on 8.2.2, and confirmed this
    fix resolves it while pointing a reproduction app 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 crash on a physical device.

@JaffaKetchup

Copy link
Copy Markdown
Member

Hey, thanks for this. Please open a bug report issue, and move your details of the issue there - this helps with tracking. The PR should only detail the fix. It would also be good to get a screen recording of you reproducing this issue.

@nisenbeck

Copy link
Copy Markdown
Contributor Author

Thanks for the quick feedback! I've split this up as requested:

Let me know if anything else is needed.

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

Really not sure how you ran into this bug in practise since it was quite difficult to reproduce, but I guess it is the sort of thing that seems to just randomly happen. I also would expect the offset never to be zero since I would've expected that to require more precision than is available, but I guess it happens.

From what I can tell, this fixes the bug. Thanks :)

@JaffaKetchup JaffaKetchup merged commit f779fae into fleaflet:master Jul 1, 2026
10 of 11 checks passed
@nisenbeck

Copy link
Copy Markdown
Contributor Author

Thanks for merging! Found it while checking whether #2199 ("LatLng is not finite") was actually resolved on 8.3.1 - was just doing normal pan/fling testing on the map and hit this and a separate issue (#2222), unrelated to #2199 itself.

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.

Fling gesture with very close extreme points causes division by zero

2 participants