Fix double detach of kept-alive children in RenderTwoDimensionalViewport by JayeshJain25 · Pull Request #194917 · flutter/flutter · GitHub
Skip to content

Fix double detach of kept-alive children in RenderTwoDimensionalViewport - #194917

Open
JayeshJain25 wants to merge 1 commit into
flutter:masterfrom
JayeshJain25:fix-two-dimensional-viewport-keepalive-double-detach-194718
Open

JayeshJain25 wants to merge 1 commit into
flutter:masterfrom
JayeshJain25:fix-two-dimensional-viewport-keepalive-double-detach-194718

Conversation

@JayeshJain25

Copy link
Copy Markdown
Contributor

A kept-alive child of RenderTwoDimensionalViewport is present in both _children and _keepAliveBucket (_cacheKeepAlives adds it to the bucket without removing it from _children). attach and detach looped over both maps, so such a child was attached/detached twice, tripping the _owner != null assertion in RenderObject.detach (e.g. a TableView inside a ListView with a selected cell scrolled away, or any cell using AutomaticKeepAliveClientMixin).

This de-duplicates the children in attach and detach. Removing the child from _children in _cacheKeepAlives was tried first but breaks existing keep-alive tests, since other code relies on it staying there.

Fixes #194718

Pre-launch Checklist


Review-Before-PR

  • Tier: T1 (forced by: N/A)
  • Languages: Dart
  • Repo health: ample test coverage
  • Reviewers: correctness, tests
  • Findings: 0 actionable · 0 fixed in-session · 0 deferred · 0 rejected · 0 rejected-critical
  • Scorer dropped: 3 · Critical verifier dropped: N/A
  • Skipped: no

A kept-alive child lives in both `_children` and `_keepAliveBucket`, so
`attach` and `detach` visited it twice and tripped the `_owner != null`
assertion (e.g. a TableView inside a ListView with a selected cell
scrolled away).

Fixes flutter#194718
@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: scrolling Viewports, list views, slivers, etc. labels Oct 21, 2026
@JayeshJain25
JayeshJain25 marked this pull request as ready for review October 21, 2026 04:18

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies RenderTwoDimensionalViewport to de-duplicate children present in both _children and _keepAliveBucket maps before calling attach or detach on them. Additionally, a regression test has been added to verify that kept-alive children are not detached twice when the viewport is removed. I have no further feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TableView inside a ListView.builder asserts '_owner != null' in RenderTwoDimensionalViewport.detach after selecting a cell and scrolling it away

1 participant