Skip to content

[widgets/raw_menu_anchor.dart] Remove listener from isScrollingNotifier on dispose.#187881

Merged
davidhicks980 merged 8 commits into
flutter:masterfrom
davidhicks980:scroll-notifier-fix
Jun 24, 2026
Merged

[widgets/raw_menu_anchor.dart] Remove listener from isScrollingNotifier on dispose.#187881
davidhicks980 merged 8 commits into
flutter:masterfrom
davidhicks980:scroll-notifier-fix

Conversation

@davidhicks980

Copy link
Copy Markdown
Contributor

This removes the isScrollingNotifier listener from RawMenuAnchor in the dispose block. This led to a memory leak, as reported by one user. This bug appears to have been present since the original MenuAnchor PR, so I'm surprised it hasn't caused more issues.

Fixes #187853

The test required a bit of gymnastics since hasListeners is protected. Let me know if it would be better to add a comment ignoring the protected status.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jun 11, 2026
@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Jun 11, 2026

@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 ensures that the ancestor scroll listener is removed and the scroll position is cleared when the root menu anchor is disposed, and adds a corresponding regression test. The reviewer suggests simplifying the test by directly checking hasListeners on the default isScrollingNotifier using // ignore: invalid_use_of_protected_member, which allows removing the custom TrackingNotifier, SpyScrollPosition, and SpyScrollController helper classes.

Comment thread packages/flutter/test/widgets/raw_menu_anchor_test.dart
Comment on lines +3745 to +3780
class TrackingNotifier extends ValueNotifier<bool> {
TrackingNotifier(super.value);
int listenerCount = 0;

@override
void addListener(VoidCallback listener) {
listenerCount++;
super.addListener(listener);
}

@override
void removeListener(VoidCallback listener) {
listenerCount--;
super.removeListener(listener);
}
}

class SpyScrollPosition extends ScrollPositionWithSingleContext {
SpyScrollPosition({required super.physics, required super.context, super.oldPosition});

final TrackingNotifier _isScrollingNotifier = TrackingNotifier(false);

@override
ValueNotifier<bool> get isScrollingNotifier => _isScrollingNotifier;
}

class SpyScrollController extends ScrollController {
@override
ScrollPosition createScrollPosition(
ScrollPhysics physics,
ScrollContext context,
ScrollPosition? oldPosition,
) {
return SpyScrollPosition(physics: physics, context: context, oldPosition: oldPosition);
}
}

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.

medium

These helper classes (TrackingNotifier, SpyScrollPosition, and SpyScrollController) are no longer needed if we use // ignore: invalid_use_of_protected_member to check hasListeners directly in the test. We can safely remove them.

References
  1. Suggest simplification and refactoring to enhance readability and maintainability. (link)

navaronbracke
navaronbracke previously approved these changes Jun 12, 2026

@navaronbracke navaronbracke 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.

LGTM with a nit about the test

Comment thread packages/flutter/test/widgets/raw_menu_anchor_test.dart
@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 13, 2026
@navaronbracke

Copy link
Copy Markdown
Contributor

Looks like Linux analyze is unhappy, though

@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 13, 2026
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jun 13, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 14, 2026
@davidhicks980

Copy link
Copy Markdown
Contributor Author

@navaronbracke My bad! I forgot to remove the dead code. Should be ready.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jun 14, 2026
@davidhicks980
davidhicks980 enabled auto-merge June 14, 2026 23:46
@davidhicks980
davidhicks980 requested a review from dkwingsmt June 14, 2026 23:47
@github-actions github-actions Bot removed the CICD Run CI/CD label Jun 15, 2026
@Piinks Piinks added the CICD Run CI/CD label Jun 23, 2026

@dkwingsmt dkwingsmt 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.

LGTM. Thanks for the quick investigation!

@dkwingsmt dkwingsmt added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 24, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 24, 2026
@auto-submit

auto-submit Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/187881, because The base commit of the PR is older than 7 days and can not be merged. Please merge the latest changes from the main into this branch and resubmit the PR.

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

Labels

CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DropdownMenu leaks parent's state when state is accessed in onSelected handler

4 participants