Repository navigation
Fix/predictive back event swipe edge out of bounds - #185140
muhammadkamel wants to merge 18 commits into
Conversation
…eEdge index Android can send a swipeEdge value of 2 (or other out-of-range values) which caused a RangeError crash since SwipeEdge only has indices 0 (left) and 1 (right). Clamp the index to the valid range to handle unknown future values gracefully.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request modifies PredictiveBackEvent.fromMap to clamp the swipeEdge index to the valid range of SwipeEdge.values, preventing RangeError exceptions when receiving unexpected values from the platform. The test suite was updated to verify clamping behavior for negative and out-of-bounds indices. A review comment suggests casting the swipeEdge value to num before converting to int to improve robustness against platform-specific serialization differences.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
0f01f2d to
3304a67
Compare
|
Hi @Piinks, Could you please review this PR? It fixes a RangeError crash in PredictiveBackEvent that occurs when the platform sends an out-of-range swipeEdge index on Android 13+. I’ve implemented defensive clamping and added regression tests to ensure stability. I am hoping to get this landed as soon as possible so it can be included in the upcoming 3.44.0 stable release. Thank you! |
…ttps://github.com/muhammadkamel/flutter into fix/predictive-back-event-swipe-edge-out-of-bounds
navaronbracke
left a comment
There was a problem hiding this comment.
Just some remarks about aligning with the specification in the docs.
…ckEvent On some Android 14+ devices, the platform sends swipeEdge=2 (BackEvent.EDGE_NONE) for button-triggered predictive back events, causing a RangeError in PredictiveBackEvent.fromMap. - Add SwipeEdge.none to the enum to explicitly represent EDGE_NONE - Map swipeEdge int values explicitly (0→left, 1→right, 2→none, _→none) instead of indexing SwipeEdge.values directly - Update isButtonEvent to return true when swipeEdge == SwipeEdge.none - Expand test coverage to 100% of PredictiveBackEvent logic Fixes: RangeError crash during predictive back on Android 14+
|
I’ve made some changes to ensure alignment with the native documentation. |
|
Hi @justinmc |
This release has already been cut.
Is there an open issue? If not please file one. We need to be able to evaluate the impact and risk before determining if we will hotfix something. |
justinmc
left a comment
There was a problem hiding this comment.
Just some nits here.
@muhammadkamel Could you file an issue as suggested by @Piinks?
…ntation for touch event handling
…BackEvent.fromMap
justinmc
left a comment
There was a problem hiding this comment.
LGTM 👍 . Thank you for the thorough issue you created @muhammadkamel!
This PR will have to wait until after the code freeze unfortunately. I'll put it in draft mode and we'll come back to it.
|
Same PR as #183642 |
|
I've marked this PR as not ready to port to flutter/packages yet. The changes in the services library will remain here after. |
|
Advice for PR splitting: Since this touches material/cupertino along with other parts of flutter/flutter, it will need to be split up. If the material/cupertino changes are dependent on the other non-material/cupertino changes in this PR, then the material/cupertino changes will need to wait for them to roll to stable before they can be ported over to material_ui/cupertino_ui in flutter/packages (which uses the stable branch).
|
|
@Piinks I've split this PR per the decoupling guidance:
Happy to open the |
Remove Material widget tests and transition comments from this PR. Those will be ported to material_ui once that package accepts PRs. Keep the minimal SwipeEdge.none exhaustiveness case in the material transition builder required for compilation after adding the enum value.
e72123a to
25755c3
Compare
|
Hey @muhammadkamel would you like to continue working on this PR? PRs marked as drafts are typically not reviewed, when you are ready, please mark it as ready for review. |
|
This pull request contains changes to Material or Cupertino, which are currently frozen in this repository. Changes should be made in Please refer to #188444 for instructions. |
There was a problem hiding this comment.
Code Review
This pull request introduces a new SwipeEdge.none enum value to represent button-triggered back events, corresponding to Android's BackEvent.EDGE_NONE. It updates PredictiveBackEvent parsing, isButtonEvent detection, and predictive back page transitions to handle this new edge type, and adds comprehensive unit tests. Feedback is provided to simplify a redundant pattern in the swipeEdge switch expression.
| swipeEdge: switch (map['swipeEdge']) { | ||
| 0 => SwipeEdge.left, | ||
| 1 => SwipeEdge.right, | ||
| 2 || _ => SwipeEdge.none, | ||
| }, |
There was a problem hiding this comment.
The pattern 2 || _ is redundant because the wildcard pattern _ already matches any value, including 2. Simplifying this to just _ makes the code cleaner, more idiomatic, and avoids potential compiler warnings about redundant patterns.
| swipeEdge: switch (map['swipeEdge']) { | |
| 0 => SwipeEdge.left, | |
| 1 => SwipeEdge.right, | |
| 2 || _ => SwipeEdge.none, | |
| }, | |
| swipeEdge: switch (map['swipeEdge']) { | |
| 0 => SwipeEdge.left, | |
| 1 => SwipeEdge.right, | |
| _ => SwipeEdge.none, | |
| }, |
References
- Suggest simplification and refactoring to enhance readability and maintainability. (link)
|
Thanks @Piinks, marked ready. One question on the code freeze check: Adding SwipeEdge.none (Android's documented BackEvent.EDGE_NONE, API 36) requires a one-line exhaustiveness update in the frozen packages/flutter/lib/src/material/predictive_back_page_transitions_builder.dart, otherwise the framework doesn't compile. No behavior change there. The material_ui side is already ported and approved: flutter/packages#12502 (its Can this compile-only change get a freeze exemption, or should the PR wait until Material is removed from the framework? |
@justinmc can advise here thanks! |
This PR addresses a RangeError in
PredictiveBackEvent.fromMapthat occurs on Android devices when the predictive back gesture provides an unexpectedswipeEdgeindex.Description
On some Android 14+ devices/ROMs, the platform can send a
swipeEdgevalue of2(BackEvent.EDGE_NONE) for button-triggered predictive back events. SinceSwipeEdgepreviously only containedleft(0) andright(1), indexingSwipeEdge.valueswith2caused a crash.This PR adds
SwipeEdge.noneto representEDGE_NONE, maps platform values explicitly infromMap, and updatesisButtonEventaccordingly. Unknown or out-of-range values are mapped toSwipeEdge.nonerather than throwing aRangeError.Decoupling split
Per the decoupling guidance, this PR is now scoped to framework/services changes only:
packages/flutter/lib/src/services/predictive_back_event.dartpackages/flutter/test/services/predictive_back_event_test.dartMaterial widget tests and transition behavior tests have been removed from this PR and will be ported to
material_uionce that package is ready to accept PRs.A minimal one-line exhaustiveness update in
predictive_back_page_transitions_builder.dart(SwipeEdge.none || null) is retained because adding the new enum value requires it for compilation.Issues Fixed
RangeErrorcrash inPredictiveBackEvent.fromMapduring back navigation on Android. Closes [Android] PredictiveBackEvent.fromMap does not guard against invalid swipeEdge value #186168Pre-launch Checklist
///).