Common Mistakes & Fixes

Frequent errors and how to solve them.

Mistake: Calling
setState() in the
build method. This will cause an infinite rebuild loop.\n\n
Fix: Only call
setState() inside event handlers like
onPressed or after an async operation completes.\n\n


\n\n
Mistake: Using a
Column or
Row inside a
ListView without wrapping it in
Expanded or giving it a fixed size. This leads to unbounded height/width errors.\n\n
Fix: If a widget inside a scrolling view doesn't have a defined size, wrap it in
Expanded to make it fill the available space, or give it an explicit size with
SizedBox or
Container.

Code Example

// BAD: Unbounded height error
ListView(
  children: [
    Column( // This Column has infinite height inside a ListView
      children: [
        Text('Item 1'),
        Text('Item 2'),
      ],
    )
  ]
)

// GOOD: Column is constrained
ListView(
  children: [
    Container( // Give it a specific height
      height: 200,
      child: Column(
        children: [
          Text('Item 1'),
          Text('Item 2'),
        ],
      ),
    )
  ]
)