removeDependent method
Called by Element.deactivate to remove the provided dependent Element from this InheritedElement.
After the dependent is removed, Element.didChangeDependencies will no longer be called on it when this InheritedElement notifies its dependents.
Subclasses can override this method to release any resources retained for
a given dependent.
Implementation
@override
void removeDependent(Element dependent) {
final ids = _stateDependents.remove(dependent);
if (ids is Set<StateId>) {
for (final id in ids) {
final subscription = _subscriptions[id];
if (subscription != null) {
subscription.watchers.remove(dependent);
subscription.readers.remove(dependent);
// Allow dependents to reattach before disposing an unused state.
if (subscription.referenceCount == 0) {
WidgetsBinding.instance.addPostFrameCallback((_) {
final isRegistered = identical(_subscriptions[id], subscription);
final isUnused = subscription.referenceCount == 0;
// Preserve subscriptions reused during reparenting and avoid disposing
// subscriptions already removed or replaced before this callback runs.
if (isRegistered && isUnused) {
_subscriptions.remove(id)?.dispose();
}
});
}
}
}
}
super.removeDependent(dependent);
}