text
stringlengths
1
474
use the lazy builder methods, with callbacks.
That ensures that only the visible portion of the
screen is built at startup time.For more information and examples, check out:<topic_end>
<topic_start>Avoid intrinsics
For information on how intrinsic passes might be causing
problems with your grids and lists, see the next section.<topic_end>
<topic_start>
Minimize layout passes caused by intrinsic operations
If you’ve done much Flutter programming, you are
probably familiar with how layout and constraints work
when creating your UI. You might even have memorized Flutter’s
basic layout rule: Constraints go down. Sizes go up.
Parent sets position.For some widgets, particularly grids and lists,
the layout process can be expensive.
Flutter strives to perform just one layout pass
over the widgets but, sometimes,
a second pass (called an intrinsic pass) is needed,
and that can slow performance.<topic_end>
<topic_start>What is an intrinsic pass?
An intrinsic pass happens when, for example,
you want all cells to have the size
of the biggest or smallest cell (or some
similar calculation that requires polling all cells).For example, consider a large grid of Cards.
A grid should have uniformly sized cells,
so the layout code performs a pass,
starting from the root of the grid (in the widget tree),
asking each card in the grid (not just the
visible cards) to return
its intrinsic size—the size
that the widget prefers, assuming no constraints.
With this information,
the framework determines a uniform cell size,
and re-visits all grid cells a second time,
telling each card what size to use.<topic_end>
<topic_start>Debugging intrinsic passes
To determine whether you have excessive intrinsic passes,
enable the Track layouts option
in DevTools (disabled by default),
and look at the app’s stack trace
to learn how many layout passes were performed.
Once you enable tracking, intrinsic timeline events
are labeled as ‘$runtimeType intrinsics’.<topic_end>
<topic_start>Avoiding intrinsic passes
You have a couple options for avoiding the intrinsic pass:To dive even deeper into how layout works,
check out the layout and rendering
section in the Flutter architectural overview.<topic_end>
<topic_start>
Build and display frames in 16ms
Since there are two separate threads for building
and rendering, you have 16ms for building,
and 16ms for rendering on a 60Hz display.
If latency is a concern,
build and display a frame in 16ms or less.
Note that means built in 8ms or less,
and rendered in 8ms or less,
for a total of 16ms or less.If your frames are rendering in well under
16ms total in profile mode,
you likely don’t have to worry about performance
even if some performance pitfalls apply,
but you should still aim to build and
render a frame as fast as possible. Why?If you are wondering why 60fps leads to a smooth visual experience,
check out the video Why 60fps?<topic_end>
<topic_start>
Pitfalls
If you need to tune your app’s performance,
or perhaps the UI isn’t as smooth as you expect,
the DevTools Performance view can help!Also, the Flutter plugin for your IDE might
be useful. In the Flutter Performance window,
enable the Show widget rebuild information check box.
This feature helps you detect when frames are
being rendered and displayed in more than 16ms.
Where possible,
the plugin provides a link to a relevant tip.The following behaviors might negatively impact
your app’s performance.Avoid using the Opacity widget,
and particularly avoid it in an animation.
Use AnimatedOpacity or FadeInImage instead.
For more information, check out
Performance considerations for opacity animation.When using an AnimatedBuilder,
avoid putting a subtree in the builder
function that builds widgets that don’t
depend on the animation. This subtree is
rebuilt for every tick of the animation.
Instead, build that part of the subtree
once and pass it as a child to
the AnimatedBuilder. For more information,
check out Performance optimizations.Avoid clipping in an animation.
If possible, pre-clip the image before animating it.Avoid using constructors with a concrete List
of children (such as Column() or ListView())
if most of the children are not visible
on screen to avoid the build cost.Avoid overriding operator == on Widget objects.
While it might seem like it would help by avoiding unnecessary rebuilds,
in practice it hurts performance because it results in O(N²) behavior.
The only exception to this rule is leaf widgets (widgets with no children),
in the specific case where comparing the properties of the widget
is likely to be significantly more efficient than rebuilding the widget
and where the widget will rarely change configuration.
Even in such cases,
it is generally preferable to rely on caching the widgets,
because even one override of operator ==
can result in across-the-board performance degradation