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

Widget priceTag() => Padding(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 6),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Color(0xFF2196F3), width: 3),
),
child: Text('12.99'),
),
);
Widget priceTag() => Padding(
padding: .symmetric(horizontal: 20, vertical: 6),
child: Container(
decoration: BoxDecoration(
borderRadius: .circular(10),
border: .all(color: Color(0xFF2196F3), width: 3),
),
child: Text('12.99'),
),
);

In argument position the rule reads the declared type of the receiving parameter, because that is what a dot shorthand resolves against. It reports only when that type is the constructed class, or declares a factory constructor of the same name that redirects to the constructed class’s constructor. EdgeInsets.all(8) passed to an EdgeInsetsGeometry parameter is reported, since EdgeInsetsGeometry.all redirects to EdgeInsets.all. A generative constructor, a static method or a non-redirecting factory of another type does not count, because the shorthand would call that member instead, with its own parameters and result. In a list or set literal the rule reads the element type the literal is given by its context: explicit type arguments, a typed variable or a parameter. An untyped final paddings = [EdgeInsets.all(8)]; is not reported, because [.all(8)] would have no type to resolve against. These are not reported:

// ShapeBorder is abstract, so `.new()` would not compile here.
ExpansionTile(shape: const Border(), title: Text('Details'));
// `.new()` would build an Object, not a Border.
void keep(Object value) {}
void store() => keep(const Border());
// A dynamic parameter gives a shorthand nothing to resolve against.
void log(dynamic value) {}
void caller() => log(EdgeInsets.all(8));
// `.only(start: 8)` would call EdgeInsetsGeometry.only, which redirects to
// EdgeInsets.only and has no `start` parameter.
Padding(
padding: EdgeInsetsDirectional.only(start: 8),
child: Text('Details'),
);
// An untyped literal gives its elements no context type.
final paddings = [EdgeInsets.all(8)];

This rule is in the opinionated preset, so it is on with preset: opinionated and every preset above it, 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.