Skip to content

avoid_border_all

v0.4.0 Warning Fix Widget Replacement

Flags usages of Border.all() which should be replaced with Border.fromBorderSide(BorderSide(...)). The Border.all() factory delegates to Border.fromBorderSide() internally, but it cannot be made const because it is a factory constructor.

Border.all() calls Border.fromBorderSide() under the hood, so using Border.fromBorderSide(BorderSide(...)) directly allows the entire expression to be const. Const objects are canonicalized at compile time, which reduces allocations and improves performance — especially in build methods that run frequently.

See also: Border | Dart lint: prefer_const_constructors

// Border.all() cannot be const
final border1 = Border.all();
// Border.all() with arguments
final border2 = Border.all(
color: const Color(0xFF3355AA),
width: 2.5,
style: BorderStyle.solid,
);
// Border.fromBorderSide() supports const
final border1 = const Border.fromBorderSide(BorderSide());
final border2 = const Border.fromBorderSide(
BorderSide(color: Color(0xFF3355AA), width: 2.5, style: BorderStyle.solid),
);

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

many_lints.yaml
rules:
avoid_border_all: true

To turn it off again:

many_lints.yaml
rules:
avoid_border_all: false

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