D
PerformanceBeginner10 XP2 min read

How does using const constructors improve Flutter performance?

TL;DR: const widgets are canonicalized at compile time — Flutter creates them once and reuses the same instance. When a parent rebuilds, const children are never rebuilt, cutting unnecessary widget tree work.

Full Answer

In Flutter, every setState() triggers build() on the widget and all its descendants. const constructors opt a subtree out of this rebuild cycle.

How it works

  • const Text('Hello') is compiled to a single canonical instance
  • On parent rebuild, Flutter sees the same object reference — skips rebuilding
  • dart compile and flutter build tree-shake unused const values
🎯

The flutter_lints package includes prefer_const_constructors and prefer_const_literals_to_create_immutables to catch missed opportunities automatically.

Code Examples

dartconst prevents unnecessary rebuilds
Output
// Only 'Count: $count' rebuilds on tap
// const widgets are identical references between rebuilds

Common Mistakes

  • Adding const to a widget that has non-const children — the outer const does nothing if children aren't const
  • Thinking const matters for StatelessWidgets with no parent setState — it only helps when parent state changes

Interview Tip

💡

Show you know it's about object identity. Flutter uses == (which defaults to reference equality for widgets) to skip rebuilds. const guarantees same reference.

#const#widget-rebuild#optimization#immutable