RenderBox was not laid out: hasSize is false

'package:flutter/src/rendering/box.dart': Failed assertion: line 1940 pos 12: 'hasSize'

Quick Answer

A RenderBox was accessed for its size before layout was completed.

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. 1

    Identify the error

    Look at the assertion failure mentioning hasSize. This means you accessed a widget's size before layout completed.

  2. 2

    Find the cause

    Check if you are reading widget size in initState or build, before the first frame is rendered.

  3. 3

    Apply the fix

    Use addPostFrameCallback to defer size reading until after the first layout pass is complete.

Bugsly 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