Skip to main content

prefer-shorthands-with-constructors

effort: 2m
dart 3.10+
configurable
has IDE fix
has auto-fix
starter+

Suggests using dot shorthand constructor invocations for the configured list of classes.

note

This rule only works for arguments (both positional and named).

⚙️ Config

Set entries (default is [EdgeInsets, BorderRadius, Radius, Border]) to configure the list of classes that should be used as dot shorthands.

analysis_options.yaml
dcm:
rules:
- prefer-shorthands-with-constructors:
entries:
- EdgeInsets
- BorderRadius
- Radius
- Border

Example

❌ Bad:

Padding(
// LINT: Prefer dot shorthands instead of explicit class instantiations.
// Try using the dot shorthand constructor.
padding: EdgeInsets.symmetric(
horizontal: horizontalPadding(screenWidth),
vertical: 12,
),
);

BoxDecoration(
color: Colors.transparent,
// LINT: Prefer dot shorthands instead of explicit class instantiations.
// Try using the dot shorthand constructor.
border: Border.all(
color: AppColors.primary.withOpacity(_controller.value),
width: 2,
),
// LINT: Prefer dot shorthands instead of explicit class instantiations.
// Try using the dot shorthand constructor.
borderRadius: BorderRadius.circular(18),
);

✅ Good:

Padding(
padding: .symmetric(
horizontal: horizontalPadding(screenWidth),
vertical: 12,
),
);

BoxDecoration(
color: Colors.transparent,
border: .all(
color: AppColors.primary.withOpacity(_controller.value),
width: 2,
),
borderRadius: .circular(18),
);