prefer_shorthands_with_constructors
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().
Why use this rule
Section titled “Why use this rule”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'));Known limitations
Section titled “Known limitations”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.
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_shorthands_with_constructors: trueTo turn it off again:
rules: prefer_shorthands_with_constructors: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: prefer_shorthands_with_constructors: classes: [EdgeInsets, BorderRadius] additional_classes: [Alignment, TextStyle]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.
Related rules
Section titled “Related rules”avoid_nested_shorthands— Avoid nesting a dot shorthand inside another dot shorthand invocation.prefer_returning_shorthands— Use dot shorthand constructors in expression function return values.prefer_shorthands_with_enums— Use dot shorthands instead of explicit enum prefixes.prefer_shorthands_with_static_fields— Use dot shorthands instead of explicit class prefixes for static fields.