prefer_spacing
This rule detects SizedBox widgets used as uniform spacers between children in a Row, Column, or Flex, and suggests using the built-in spacing argument instead. It also catches separatedBy() and .expand() patterns that insert SizedBox spacers. Requires Flutter 3.27+.
Why use this rule
Section titled “Why use this rule”Scattering SizedBox(height: 10) between every child is verbose and easy to get wrong (miss one, use a different value, etc.). The spacing parameter on Row, Column, and Flex applies uniform spacing declaratively with a single property, making the intent clear and the code shorter. It also avoids polluting the children list with non-semantic spacer widgets.
See also: Column spacing
Column( children: [ Text('First'), SizedBox(height: 10), Text('Second'), SizedBox(height: 10), Text('Third'), ],)Column( spacing: 10, children: [Text('First'), Text('Second'), Text('Third')],)Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_spacing: trueTo turn it off again:
rules: prefer_spacing: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: prefer_spacing: min_children: 5rules: prefer_spacing: min_children: 5| Option | Type | Default | Description |
|---|---|---|---|
min_children |
int | 3 |
Minimum number of children in the list before spacers are reported |
Related rules
Section titled “Related rules”use_gap— Opposing convention. Use Gap widget instead of SizedBox for spacing in multi-child widgets.avoid_expanded_as_spacer— Use Spacer instead of Expanded with an empty child.always_pass_global_key— Don’t create a GlobalKey inside build.avoid_conditional_hooks— Never call hooks inside conditionals, loops, or ternaries.