D
Flutter CoreEasy20 XP2 min read

What is tree shaking in Flutter and how does it work?

TL;DR: Tree shaking is the Dart compiler's dead-code elimination pass during AOT release builds — it removes classes, methods, and constants that are never referenced, significantly reducing the binary size.

Full Answer

The Dart AOT compiler performs whole-program analysis at build time to identify code that's unreachable from main(). Any unreachable code — classes never instantiated, methods never called, constants never accessed — is dropped from the final binary.

  • Only works in AOT (Release) builds, not JIT (Debug)
  • Icons: Using Icons.abc only includes that icon's SVG path, not the whole Material icon font
  • Packages: Unused exports from packages are eliminated
  • The result: release APKs are often 60-80% smaller than debug builds
🎯

To benefit from icon tree-shaking, use flutter_lints and the constant Icons class. Avoid dynamic icon loading (Icons.values) which defeats tree-shaking.

Interview Tip

💡

Dart's tree shaking is more aggressive than JS bundlers because Dart is statically typed and AOT-compiled — the compiler has complete knowledge of the call graph at build time.

#tree-shaking#aot#bundle-size#optimization