Why This Happens
In Flutter, this assertion failure occurs when you try to get the size of a widget before Flutter has completed the layout pass. This often happens when using GlobalKey to get a widget's size during initState or build, before the first frame is rendered.
The Problem
final key = GlobalKey();
@override
void initState() {
super.initState();
final size = key.currentContext!.size; // Not laid out yet!
}The Fix
final key = GlobalKey();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
final size = key.currentContext?.size;
if (size != null) {
setState(() { _widgetSize = size; });
}
});
}Step-by-Step Fix
- 1
Identify the error
Look at the assertion failure mentioning hasSize. This means you accessed a widget's size before layout completed.
- 2
Find the cause
Check if you are reading widget size in initState or build, before the first frame is rendered.
- 3
Apply the fix
Use addPostFrameCallback to defer size reading until after the first layout pass is complete.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free