Skip to content

Conversation

@chunhtai
Copy link
Contributor

@chunhtai chunhtai commented Feb 7, 2022

The current logic causes problem when there are overlay involve. The owner of an overlay entry is usually a State of a statefulwidget. The problem occur when the overlay entry usually out-lived the owner for one frame. so it is very hard to figure out the clean up order if there are change notifiers involved. This PR allows removeListen calls to a disposed change notifier.

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 Feb 7, 2022
@chunhtai chunhtai requested review from Hixie and goderbauer February 7, 2022 23:15
class ChangeNotifier implements Listenable {
int _count = 0;
List<VoidCallback?> _listeners = List<VoidCallback?>.filled(0, null);
late List<VoidCallback?> _listeners;
Copy link
Member

Choose a reason for hiding this comment

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

Why move this to late?

For some background: This was carefully designed with performance in mind and I suspect that late will make this slightly slower. (Unless we have a benchmark to prove otherwise...)

@override
void removeListener(VoidCallback listener) {
assert(_debugAssertNotDisposed());
if (_debugDisposed)
Copy link
Member

Choose a reason for hiding this comment

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

_debug variables should only be sued in debug contexts (i.e. other debug methods or asserts).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch i totally forgot this

_debugDisposed = true;
return true;
}());
_listeners = const <VoidCallback?>[];
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good, I will also leave a comment as the original pr suggested..

_debugDisposed = true;
return true;
}());
_listeners = const <VoidCallback?>[];
Copy link
Member

Choose a reason for hiding this comment

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

Set _count to 0 as well and then the for loop in removeListener would just immediately terminate without the need for the _debugDisposed guard?

@chunhtai chunhtai requested a review from goderbauer February 8, 2022 18:52
@chunhtai
Copy link
Contributor Author

chunhtai commented Feb 9, 2022

a friendly bump

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

List<VoidCallback?> _listeners = List<VoidCallback?>.filled(0, null);
// The _listeners is intentionally set to a fixed-length _GrowableList instead
// of const [] for performance reasons.
// See https://github.com/flutter/flutter/pull/71947/files#r545722476 for
Copy link
Contributor

Choose a reason for hiding this comment

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

can we inline the explanation here rather than linking to github? the code will probably outlive that link.

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.

4 participants