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() is a factory, and a factory can never be const. It forwards straight to Border.fromBorderSide(BorderSide(...)), which can. Spelling out the second form lets the whole BoxDecoration around it become constant, so it is built once at compile time instead of on every build.

See also: Border | Dart lint: prefer_const_constructors

// A bordered field, reallocated on every frame.
Container(
decoration: BoxDecoration(
border: Border.all(color: const Color(0xFF3355AA), width: 2), // LINT
),
child: const Text('Hello'),
);
Container(
decoration: const BoxDecoration(
border: Border.fromBorderSide(
BorderSide(color: Color(0xFF3355AA), width: 2),
),
),
child: const Text('Hello'),
);

Border.all() with no arguments is a 1px black border. Its explicit form is BorderSide(), whose defaults are the same:

// Don't
final border = Border.all();
// Do
const border = Border.fromBorderSide(BorderSide());

Once const-capable, the border can live outside build and be shared by every call site:

// Don't — a fresh Border on every call site, on every rebuild
BoxDecoration(border: Border.all(color: const Color(0xFFCCCCCC)));
// Do
const kFieldBorder = Border.fromBorderSide(
BorderSide(color: Color(0xFFCCCCCC)),
);
const BoxDecoration(border: kFieldBorder);
// Don't
final border = Border.all(color: theme.dividerColor);
// Do
final border = Border.fromBorderSide(BorderSide(color: theme.dividerColor));

theme.dividerColor is not a compile-time constant, so const is unavailable here — but the explicit form is what the rule asks for, and it becomes const the moment the colour does.

The fix does not add const for you. It rewrites Border.all(args) into Border.fromBorderSide(BorderSide(args)) and stops. Adding the keyword is the SDK’s prefer_const_constructors job — turn that on to collect the other half of the win.

Only Border.all is matched, because it is the only Border constructor that is a factory. Border.fromBorderSide, Border.symmetric and the default Border(...) are all already const, so there is nothing to report.

Border.all on a subclass is not matched. The check is for an expression whose static type is exactly Border; a Border subclass of your own with its own all factory is left alone.

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.