Skip to content

Conversation

@LongCatIsLooong
Copy link
Contributor

There're customers using OverlayEntry.addListener (and leaking listeners unfortunately).
Alternatively we can let _OverlayEntryWidgetState own the value notifier (so it doesn't leak since it will always be disposed by the state object). Old listeners won't be notified when the entry is reused to build new widgets but our internal customer and route entry are only interested in the mounted state of the current widget.
Fixes #102718

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Apr 28, 2022
@LongCatIsLooong LongCatIsLooong force-pushed the defer-OverlayEntry-dispose branch from 639ff35 to 60560bb Compare April 28, 2022 23:43

/// Whether the `_OverlayState`s built using this [OverlayEntry] is currently
/// mounted.
final ValueNotifier<bool> _overlayStateMounted = ValueNotifier<bool>(false);
Copy link
Member

Choose a reason for hiding this comment

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

With this change, we don't need to extend ChangeNotifier anymore, right? There's nothing from it that this class uses, AFIKT. We could just implement the Listenable interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah only the docs but I think I overrode the docs for dispose anyways.

}

void _didUnmount() {
if (_disposedByOwner && !mounted) {
Copy link
Member

Choose a reason for hiding this comment

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

from the method name (_didUnmount) it should follow that mounted must always be false here? Should that !mounted just be in an assert?


void _didUnmount() {
if (_disposedByOwner && !mounted) {
super.dispose();
Copy link
Member

Choose a reason for hiding this comment

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

No functionality of the superclass appears to be used in this implementation, so there is really no reason to call its dispose method... Technically, you should dispose the _overlayStateMounted, though.

Copy link
Member

Choose a reason for hiding this comment

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

(see also the other comment about removing the super class altogether)

assert(_overlay == null, 'An OverlayEntry must first be removed from the Overlay before dispose is called.');
_disposedByOwner = true;
if (!mounted) {
super.dispose();
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

LGTM

@fluttergithubbot fluttergithubbot merged commit 0b3c5d1 into flutter:master Apr 29, 2022
@LongCatIsLooong LongCatIsLooong deleted the defer-OverlayEntry-dispose branch April 29, 2022 19:49
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 29, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Apr 29, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 29, 2022
egramond pushed a commit to egramond/flutter that referenced this pull request May 5, 2022
github-merge-queue bot pushed a commit that referenced this pull request Nov 25, 2025
## Description

This PR adds an assert message to help users understand the error
occuring when `OverlayEntry.remove` is called twice. It also adds a
comment about calling dispose after removal as this is mandatory since
github.com//issues/102794.

## Related Issue

Fixes ["Failed assertion: line 207 pos 12: '_overlay != null': is not
true" when trying to remove a non null
overlayEntry](#145466)
Related external issue:
LanarsInc/top-snackbar-flutter#80

## Tests

- Adds 1 test.
mboetger pushed a commit to mboetger/flutter that referenced this pull request Dec 2, 2025
…er#178163)

## Description

This PR adds an assert message to help users understand the error
occuring when `OverlayEntry.remove` is called twice. It also adds a
comment about calling dispose after removal as this is mandatory since
github.com/flutter/issues/102794.

## Related Issue

Fixes ["Failed assertion: line 207 pos 12: '_overlay != null': is not
true" when trying to remove a non null
overlayEntry](flutter#145466)
Related external issue:
LanarsInc/top-snackbar-flutter#80

## Tests

- Adds 1 test.
reidbaker pushed a commit to AbdeMohlbi/flutter that referenced this pull request Dec 10, 2025
…er#178163)

## Description

This PR adds an assert message to help users understand the error
occuring when `OverlayEntry.remove` is called twice. It also adds a
comment about calling dispose after removal as this is mandatory since
github.com/flutter/issues/102794.

## Related Issue

Fixes ["Failed assertion: line 207 pos 12: '_overlay != null': is not
true" when trying to remove a non null
overlayEntry](flutter#145466)
Related external issue:
LanarsInc/top-snackbar-flutter#80

## Tests

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

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling dispose() on OverlayEntry throws an assertion error

3 participants