Const Constructors vs Runtime Instantiation: When to Prioritize Immutable Widgets
0 reputation · 27 Oct 2022, 11:17 UTC
Performance Measurement Before Optimization
The goal is to quantify whether converting a widget to a const constructor actually reduces frame times in a real Flutter 3.x app. The trade‑off lies between the compile‑time guarantees of const and the flexibility of runtime instantiation when widget data is dynamic.
Constraints and Uncertainty
1. const can only be applied to compile‑time constants; any stateful property must be final and known at compile time.\n2. A const widget instance is reused across rebuilds, which can lead to stale UI if an inherited widget changes.\n3. DevTools Performance tab can surface slow frames, but interpreting the impact of const versus non‑const requires careful isolation.
Specific Questions
- Does using
constconstructors provide a measurable frame‑time improvement for complex widget trees in Flutter 3.x? - Under what runtime conditions does a
constwidget fail to rebuild when its parent state changes? - Is there a systematic DevTools approach to detect stale UI caused by
constwidgets?