fix: prevent NaN fling direction when tracked pointer offsets are zero#2220
Conversation
|
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. |
|
Thanks for the quick feedback! I've split this up as requested:
Let me know if anything else is needed. |
JaffaKetchup
left a comment
There was a problem hiding this comment.
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 :)
Fixes #2221 - see that issue for the full bug report,
reproduction steps and root-cause analysis.
Fix
MapInteractiveViewerState._handleScaleEndcould divideflingOffsetby azero
flingDistance(0.0 / 0.0 = NaN) when both the final tracked pointersegment 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
flingDirectionhelper, and added a final fallback to the (alreadyguaranteed non-zero) velocity direction instead of dividing by zero:
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).
velocityDirectionis guaranteed finite:magnitudeis already checked tobe
>= _kMinFlingVelocitybefore this is called, and Flutter's ownVelocityTracker/LeastSquaresSolvernever returnNaN- they fall backto
Offset.zero/nullon ill-conditioned samples - so this can neveritself be a source of
NaN.Testing
test/gestures/map_interactive_viewer_test.dartcovering all threebranches of
flingDirection, including a regression test for thezero/zero case.
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 diagnosethe 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.