Skip to content

avoid_expanded_as_spacer

v0.4.0 Warning Fix Widget Replacement

Flags Expanded widgets that wrap an empty SizedBox or Container as their child. This pattern is equivalent to using the Spacer widget, which is purpose-built for this exact use case.

Flutter provides the Spacer widget specifically for creating flexible space in Row, Column, and Flex layouts. Using Expanded with an empty child obscures the intent and adds an unnecessary widget to the tree. Spacer is clearer, more concise, and immediately communicates that the purpose is to fill available space.

See also: Spacer | Expanded

// Expanded with empty SizedBox
const Expanded(child: SizedBox());
// Expanded with empty Container
Expanded(child: Container());
// Expanded with flex and empty SizedBox
const Expanded(flex: 2, child: SizedBox());
// Use Spacer directly
const Spacer();
// Use Spacer with flex parameter
const Spacer(flex: 2);
// Expanded with a non-empty child is fine
const Expanded(child: Text('content'));

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_expanded_as_spacer: false