fix(navigation): clamp ViewGroup tab index when tab list shrinks#2225
Merged
Conversation
Restoring a stored view-group index after resume or definition updates could leave viewGroupNotifier pointing past pagePayloads, causing RangeError in drawer, sidebar, and bottom-nav builds. Clamp at read sites and resync the notifier post-frame when out of range; clamp PageController initialPage too. Co-authored-by: Sharjeel Yunus <sharjeelyunus@users.noreply.github.com>
sharjeelyunus
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a crash when the global
viewGroupNotifierholds a tab index that is no longer valid for the currentpagePayloadslist (for example after_restoreVisibleScreenOnForegroundreapplies a stored index from before the app backgrounded, while the live definition now exposes fewer tabs). Drawer, sidebar, and bottom-nav code paths indexedpagePayloads[viewGroupNotifier.viewIndex]and used that value forIndexedStack/PageController.initialPagewithout bounds checks, which producedRangeErrorand broke the app.Related Issue
None filed; found by tracing
_restoreVisibleScreenOnForegroundinensemble_provider.dartand the ViewGroup build paths.Type of Change
What Has Changed
safeViewGroupPayloadIndexinpage_group.dartand used it wherever the UI indexespagePayloadsor drivesIndexedStackby tab index.updatePagewith the clamped index so persistence and navigation stay aligned.BottomNavPageGroup/PageGroupWidgetWrappernow clampPageController.initialPageand reload-view body indexing using the same helper;PageGroupWidgetWrappertakespageCountfor clamping.safeViewGroupPayloadIndex.How to Test
modules/ensemble, runflutter test test/safe_view_group_payload_index_test.dart.Screenshots / Videos
N/A
Checklist
flutter analyzeand addressed any new warningsflutter testand all tests passNote: The automation sandbox did not have the Flutter SDK on
PATH, soflutter test/flutter analyzewere not executed here. CI or a local run is appreciated.Duplicate check
Reviewed open and recent PRs with
gh pr list(EnsembleUI/ensemble). Open PR #2220 clampsnavigateViewGroupaction indices only; it does not cover the background-restore path or defensive indexing inPageGroupState/BottomNavPageGroup, so this is not a duplicate of that work. Open #2215 addressesListViewCorescroll-controller swapping (unrelated). No existing branch or PR fixes the same stale-index-after-resume trigger forpagePayloadsindexing.