Skip to content

prefer_shorthands_with_constructors

v0.3.0WarningFixConfigurableShorthand Patterns

Flags explicit constructor invocations of EdgeInsets, BorderRadius, Radius, and Border in argument or collection-literal position. In those positions the class name is usually redundant and can be replaced with a dot shorthand like .all(), .symmetric(), or .circular().

These Flutter classes appear frequently in widget trees, and their constructors are often passed as named arguments where the type is already known. Replacing EdgeInsets.all(8) with .all(8) reduces visual clutter in deeply nested build methods, making the widget tree easier to scan.

See also: Dart language - Constructor tear-offs

Padding(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 6),
child: Text('Hello'),
);
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue, width: 3),
),
);
Padding(padding: EdgeInsets.all(8), child: Text('World'));
Padding(
padding: .symmetric(horizontal: 20, vertical: 6),
child: Text('Hello'),
);
Container(
decoration: BoxDecoration(
borderRadius: .circular(10),
border: .all(color: Colors.blue, width: 3),
),
);
Padding(padding: .all(8), child: Text('World'));

The rule does not resolve the declared type of the destination parameter. In argument position it only checks the constructed expression’s own type, so it reports any of the four supported classes appearing there — even when the receiving parameter is dynamic or Object, where a dot shorthand has no context type and would not compile. If a parameter is untyped, keep the explicit class name and suppress the diagnostic on that line.

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_shorthands_with_constructors: true

To turn it off again:

many_lints.yaml
rules:
prefer_shorthands_with_constructors: false

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
many_lints:
rules:
prefer_shorthands_with_constructors:
classes: [EdgeInsets, BorderRadius]
additional_classes: [Alignment, TextStyle]
Option Type Default Description
classes list of strings [EdgeInsets, BorderRadius, Radius, Border] Replaces the default class list outright
additional_classes list of strings [] Extends whichever list applies

Use additional_classes when you want the defaults plus your own — restating every default in classes means your config silently misses any class added in a later release. Set classes: [] to disable the rule for all classes (though enabled: false is clearer for that).

Common additions: Alignment, AlignmentDirectional, EdgeInsetsGeometry, TextStyle.