Defer the OverlayEntry listenable disposal until its widget is unmounted#102794
Conversation
639ff35 to
60560bb
Compare
|
|
||
| /// Whether the `_OverlayState`s built using this [OverlayEntry] is currently | ||
| /// mounted. | ||
| final ValueNotifier<bool> _overlayStateMounted = ValueNotifier<bool>(false); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yeah only the docs but I think I overrode the docs for dispose anyways.
| } | ||
|
|
||
| void _didUnmount() { | ||
| if (_disposedByOwner && !mounted) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
(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(); |
## 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.
…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.
…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.
There're customers using
OverlayEntry.addListener(and leaking listeners unfortunately).Alternatively we can let
_OverlayEntryWidgetStateown 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
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.