Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter reprojection pattern

What is a reprojection pattern/member variable talked about in the paragraph below? What is an example of that? From Flutter official docs: "Because widgets are immutable, if an element has not marked itself as dirty, the element can return immediately from build, cutting off the walk, if the parent rebuilds the element with an identical widget. Moreover, the element need only compare the object identity of the two widget references in order to establish that the new widget is the same as the old widget. Developers exploit this optimization to implement the reprojection pattern, in which a widget includes a prebuilt child widget stored as a member variable in its build."

like image 716
May Avatar asked Aug 14 '26 07:08

May


1 Answers

I was able to find an answer to this thanks to @Hixie on this Github issue.

..."reprojection", where a widget was passed in from a parent and is being then passed down again to a child. See for example the child property of AnimatedBuilder.

If we take a look at the docs for AnimatedBuilder as recommended, there is a heading titled 'Performance optimizations' that describes reprojection as follows:

If the builder function contains a subtree that does not depend on the animation passed to the constructor, it's more efficient to build that subtree once instead of rebuilding it on every animation tick.

From this, we can see that 'reprojection' is when we pre-build a portion of the widget tree and pass it as a parameter to a widget that is frequently rebuilt. That way, each time the widget is rebuilt, it can shortcut building the pre-built portion of its widget tree.

AnimatedBuilder(
  animation: _controller,
  child: <pre-built widget tree>,
  builder: (BuildContext context, Widget? child) {
    return <widget to rebuild> + child;
  },
);
like image 107
Teran Peterson Avatar answered Aug 19 '26 16:08

Teran Peterson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!