Const Constructors vs Runtime Instantiation: When to Prioritize Immutable Widgets
0 reputation · 27 Oct 2022, 11:17 UTC
0 reputation · 27 Oct 2022, 11:17 UTC
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.
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.
const constructors provide a measurable frame‑time improvement for complex widget trees in Flutter 3.x?const widget fail to rebuild when its parent state changes?const widgets?Using const constructors can reduce frame‑time overhead in Flutter 3.x, but only when the widget subtree is composed entirely of compile‑time constants. If any field depends on runtime data or the subtree reads an InheritedWidget that may change, the const instance will not rebuild and can cause stale UI.
const widget is created once at compile time and reused across rebuilds, skipping the widget‑creation and layout passes for that subtree.final and have values known at compile time (literals, other const objects, or static const fields).const widget references an InheritedWidget (directly or indirectly) whose value can change, the widget will not receive the update because the same instance is reused.debugShowPerformanceOverlay: true) can measure frame‑time differences when the rest of the app is held constant.The performance gain comes from avoiding the allocation, initialization, and layout of the widget subtree on each frame. In deep or frequently rebuilt trees (e.g., large lists, grids, or custom paint hierarchies) this can shave a few milliseconds off the frame budget. In shallow trees the effect is usually negligible because the overhead of widget creation is already small.
Identify a widget subtree that is rebuilt often and whose constructor arguments are all compile‑time constants (e.g., static icons, static text, fixed sizes).
Wrap that subtree with a const constructor:
const MyConstantSubtree(
Key? key,
) : super(key: key);
Run the app in profile mode (flutter run --profile) and enable the performance overlay:
MaterialApp(
debugShowPerformanceOverlay: true,
home: MyHomePage(),
);
Record the average frame time (ms) shown in the overlay for a stable interval (e.g., 10 seconds).
Remove the const qualifier, rebuild, and repeat the measurement under identical conditions.
Compare the two averages; a consistent reduction indicates a measurable benefit.
const Widgets Fail to RebuildA const widget will not rebuild if:
const widget’s fields change (the instance is reused).InheritedWidget whose value can change; because the same instance is reused, the new value is ignored, leading to stale UI.InheritedWidget (e.g., toggle a theme, change locale).const subtree receives a highlight. If it does not, the widget is likely stale.print or logger in the widget’s build method to confirm it is not being called.To decide whether to apply const safely, we need to know: Does the widget subtree (or any of its descendants) depend on an InheritedWidget whose value can change at runtime? If the answer is yes, const should be avoided or the subtree should be split so that only the truly constant parts are const.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.