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
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'), ),);Known limitations
Section titled “Known limitations”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)];Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset, so it is on with
preset: opinionated and every preset above it, 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.