1
  /// Whether the framework should notify widgets that inherit from this widget.
  ///
  /// When this widget is rebuilt, sometimes we need to rebuild the widgets that
  /// inherit from this widget but sometimes we do not. For example, if the data
  /// held by this widget is the same as the data held by `oldWidget`, then we
  /// do not need to rebuild the widgets that inherited the data held by
  /// `oldWidget`.
  ///
  /// The framework distinguishes these cases by calling this function with the
  /// widget that previously occupied this location in the tree as an argument.
  /// The given widget is guaranteed to have the same [runtimeType] as this
  /// object.
  @protected
  bool updateShouldNotify(covariant InheritedWidget oldWidget);

The official comment of InheritedWidget says that : when the InheritedWidget is rebuilt, the dependant widgets can choose to rebuild or not.

But in my understanding, this InheritedWidget is the parent widgets to all the dependant widgets ,and if the parent widgets is rebuild , it will make all the child widgets rebuild recursively.

Is my understanding of rebuild wrong?

ximmyxiao
  • 2,622
  • 3
  • 20
  • 35

1 Answers1

1

In Flutter, the implementation of the child widgets and how the parent widget utilises them determine whether a parent widget's rebuild will cause the child widgets to rebuild.

When the parent widget rebuilds, the child widgets do not need to be rebuilt if they are defined using 'const' constructors and their properties remain constant.

The child widgets will rebuild when the parent widget does, though, if they depend on mutable data or have non-const attributes (such as those used by'setState'). In certain circumstances, employing "Keys" or encasing the child widget in a "ValueListenableBuilder" or "StreamBuilder" widget that listens for particular data updates will optimise the rebuild process.

You can employ a few strategies to prevent rebuilding child widgets when the parent widget rebuilds:

  1. When using 'const' for child widgets.
  2. Employ a StatefulWidget with a state of its own.
  3. Make use of the optimisation widgets that Flutter includes by default, such as "ValueListenableBuilder," "StreamBuilder," or "Provider."

Remember that only the sections of a widget tree that have changed are rebuilt by Flutter, which is often effective at doing so. The most of the time, changing a parent widget won't significantly affect performance, but it's always a good idea to optimise.

tk_tanz
  • 384
  • 2
  • 7
  • Thanks @tk_tanz, With widget tree for an example: InheritedWidgetA->WidgetB->WidgetC , If InheritedWidgetA is rebuild , will WidgetB and WIdgetC rebuild ? – ximmyxiao May 29 '23 at 07:06
  • Unless you mark Widget B and Widget C as const, it will rebuild – tk_tanz May 29 '23 at 13:13
  • ,so the comment of code "/// When this widget is rebuilt, sometimes we need to rebuild the widgets that /// inherit from this widget but sometimes we do not." seems not right , because the widget inherit from will rebuild always after the inherited widget – ximmyxiao May 30 '23 at 01:45