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.
Why use this rule
Section titled “Why use this rule”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.
// Expanded with empty SizedBoxconst Expanded(child: SizedBox());
// Expanded with empty ContainerExpanded(child: Container());
// Expanded with flex and empty SizedBoxconst Expanded(flex: 2, child: SizedBox());// Use Spacer directlyconst Spacer();
// Use Spacer with flex parameterconst Spacer(flex: 2);
// Expanded with a non-empty child is fineconst Expanded(child: Text('content'));Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
avoid_expanded_as_spacer: true.
To turn it off:
rules: avoid_expanded_as_spacer: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_spacing— Use the spacing argument on Row/Column instead of SizedBox spacers.use_gap— Use Gap widget instead of SizedBox for spacing in multi-child widgets.avoid_border_all— Use Border.fromBorderSide instead of Border.all for const support.avoid_incorrect_image_opacity— Use Image’s opacity parameter instead of wrapping in Opacity.