Skip to content
tezvyn:

Isolating and reducing excessive widget rebuilds

Source: interviewMediumHow cards are made

Summary

narrowing rebuild scope.

Key points

push state down, use const subtrees, rebuild only listeners with ValueListenableBuilder, isolate repaints with RepaintBoundary.

What's really being asked

Whether you can distinguish the build, layout and paint phases and apply targeted techniques to shrink the blast radius of a state change.

The full answer

First diagnose with the DevTools performance view and the rebuild profiler to see which widgets rebuild and how often. Then reduce scope. Push state down so setState lives in the smallest widget that owns it, not a high ancestor. Mark unchanging subtrees as const so they are skipped during rebuilds. For a single value, use ValueListenableBuilder or, with Provider, Selector, so only the builder closure rebuilds rather than the parent. Pull expensive painting, like a chart or shader, behind a RepaintBoundary so the compositor caches its layer and a neighbor's repaint does not invalidate it. Use builders like ListView.builder for lazy construction.

The mistakes people make

Wrapping the entire tree in RepaintBoundary, which costs memory and composition without targeting the cause. Confusing a rebuild, which is element work, with a repaint, which is canvas work. Using setState at the top of the screen for a tiny toggle. Believing const is purely cosmetic.

What usually comes next

What exactly does RepaintBoundary isolate versus ValueListenableBuilder. How does const cut rebuilds. When does a rebuild not cause a repaint. How do you measure improvement.

A concrete example

A dashboard rebuilds the full screen each second to update one clock label, dragging a heavy chart along. The fix wraps the clock in a ValueListenableBuilder driven by a ValueNotifier so only the label rebuilds, marks the static header const, and wraps the chart in a RepaintBoundary so its cached layer survives the label updates. Frame times drop because the costly subtree no longer participates in the per-second update.

Interview question

A screen rebuilds entirely every second to update one label, also re-running a heavy chart. Which combination best targets the problem?

  • a.Drive the label with ValueListenableBuilder and isolate the chart with RepaintBoundaryCorrect
  • b.Wrap the whole screen in a single RepaintBoundary
  • c.Replace all widgets with StatelessWidget to stop rebuilds
  • d.Call setState on the root widget more efficiently
Why?

Confining the changing label to a ValueListenableBuilder rebuilds only the label, and a RepaintBoundary stops the chart's layer from being invalidated. A single top-level RepaintBoundary does not reduce the rebuild scope of the label change.

Just read this? Test yourself on what you have been reading.

Read the original → docs.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles