DA

Dart/Flutter Performance

Flutter widget rebuilds, lazy rendering, and profiling patterns for Dart/Flutter apps

Details

Language / Topic
dartDart
Category
Performance

Rules

balanced
- Use `const` constructors for widgets that don't change — Flutter skips rebuilding const widgets entirely.
- Minimize `setState()` scope — only rebuild the smallest widget subtree that actually changes, or use state management (Riverpod, Bloc).
- Use `ListView.builder` for long lists instead of `ListView` — it lazily builds only visible items for O(1) memory usage.
- Mark widgets `const` to eliminate rebuilds: `const Text('Hello')` is created once and reused across all parent rebuilds — only apply to widgets whose parameters never change.
- Scope `setState()` to the smallest possible widget: extract mutable parts into a child `StatefulWidget` so only that subtree rebuilds when state changes, not the entire screen.
- Use `ListView.builder(itemCount: items.length, itemBuilder: (ctx, i) => ItemTile(items[i]))` for lists of 50+ items — it builds and destroys tiles lazily as the user scrolls.
- Use `RepaintBoundary` around widgets with frequent, independent repaints (e.g. animated counters) to isolate their repaint layer from the parent.
- Profile in release mode with Flutter DevTools → Performance tab: check for jank (frames > 16ms) and identify which widgets trigger expensive rebuilds with the Widget Rebuild tracker.