BeginnerFlutter Performance

Why should you use const widgets in Flutter?

Short Answer

A `const` widget is created once at compile time and reused across rebuilds — Flutter can skip rebuilding it entirely because it knows the instance can never change.

When a widget's constructor and all its arguments are compile-time constants, marking it `const` tells Dart to create exactly one instance, shared everywhere it's used. During a rebuild, Flutter compares the new widget instance to the old one; if they're `==` (which `const` instances are, by identity), it skips rebuilding that subtree entirely instead of diffing it.

This matters most for static decoration — icons, dividers, fixed-text labels — nested deep inside frequently-rebuilding parents. It costs nothing to add and the Dart analyzer (`prefer_const_constructors` lint) will even suggest it for you.

Code Example

// Rebuilt fresh every time the parent rebuilds:
Icon(Icons.star, color: Colors.amber)

// Created once, reused forever:
const Icon(Icons.star, color: Colors.amber)

Common Mistakes

  • ×Not marking a widget `const` just because it "looks dynamic" when its actual arguments are all compile-time constants.
  • ×Wrapping a `const`-eligible subtree in a non-const parent and assuming the optimization still applies — the analyzer will flag if it doesn't propagate.