Skip to content

Unexpected behaviour for InheritedElement.update #43

@nielsenko

Description

@nielsenko

I think I have found a bug, or at least I was surprised to realize that update on InheritedElement is implemented like:

@override
void update(covariant Component newComponent) {
  final InheritedComponent oldComponent = component;
  super.update(newComponent);
  if (component.updateShouldNotify(oldComponent)) {
    notifyClients(oldComponent);
  }
  // Rebuild to update the child widget tree (like Flutter's ProxyElement)
  rebuild();  // <-- THIS IS THE PROBLEM
}

This caused me some performance problems. Still does, if I use the build-in theme detection of NoctermApp.

The Issue

rebuild() is called unconditionally after every update(), regardless of whether updateShouldNotify() returns true or false. This causes BuildableElement.performRebuild() to run, which calls updateChild() on the entire subtree.

Flutter's Approach

In Flutter, InheritedElement extends ProxyElement, which handles child updates more efficiently:

  • ProxyElement.update() directly calls updateChild() on the child
  • ProxyElement.performRebuild() only propagates to children if explicitly called
  • The child element is updated in-place when widgets match, not rebuilt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions