Rules / Performance / prefer-declaring-const-constructor

prefer-declaring-const-constructor

lint/performance/prefer-declaring-const-constructor
recommendedflutter
Same as prefer_declaring_const_constructor · Pyramid Lint

Flags a constructor that could be declared const but is not.

A const constructor lets callers create const instances, which Flutter canonicalizes and skips rebuilding — a real win for widgets. The rule fires when a class could support one: all fields are final (or const) with const-evaluable initializers, the class extends nothing but Object and applies no mixins (whose const-ness is unknowable without resolution), and the constructor is non-factory, non-external, has no body, and carries only const-evaluable field initializers. Super and redirecting initializers block the suggestion conservatively, since the target's const-ness cannot be proven.

Invalid

example.dartdart
// Bad: constructor could be const but isn't
class Point {
  final int x;
  final int y;

  Point(this.x, this.y);
}

// Bad: immutable class with non-const constructor
class Color {
  final int red;
  final int green;
  final int blue;

  Color(this.red, this.green, this.blue);
}

// Bad: nested final field class without const
class Coordinate {
  final double latitude;
  final double longitude;
  final String label;

  Coordinate(this.latitude, this.longitude, this.label);
}

// Bad: immutable model with final fields
class Size {
  final double width;
  final double height;

  Size(this.width, this.height);
}

// Bad: all-final constructor
class Duration {
  final int seconds;
  final int milliseconds;

  Duration(this.seconds, this.milliseconds);
}

// Bad: final fields with const-evaluable literal initializers are eligible.
class Flags {
  final bool enabled = true;
  final int count = 0;

  Flags();
}
Constructor could be declared as const.
5
6 Point(this.x, this.y);
Constructor could be declared as const.
14
15 Color(this.red, this.green, this.blue);
Constructor could be declared as const.
23
24 Coordinate(this.latitude, this.longitude, this.label);
Constructor could be declared as const.
31
32 Size(this.width, this.height);
Constructor could be declared as const.
39
40 Duration(this.seconds, this.milliseconds);
Constructor could be declared as const.
47
48 Flags();

Valid

example.dartdart
// Good: const constructor on immutable class
class Point {
  final int x;
  final int y;

  const Point(this.x, this.y);
}

// Good: const constructor for color
class Color {
  final int red;
  final int green;
  final int blue;

  const Color(this.red, this.green, this.blue);
}

// Good: const constructor for immutable coordinate
class Coordinate {
  final double latitude;
  final double longitude;
  final String label;

  const Coordinate(this.latitude, this.longitude, this.label);
}

// Good: mutable class doesn't need const (has non-final fields)
class Widget {
  final String name;
  late String _value;

  Widget(this.name);
}

// Good: const constructor for immutable size
class Size {
  final double width;
  final double height;

  const Size(this.width, this.height);
}

// Good: const constructor for immutable duration
class Duration {
  final int seconds;
  final int milliseconds;

  const Duration(this.seconds, this.milliseconds);
}

// Good: factory constructor (not required to be const)
class Pair {
  final int first;
  final int second;

  const Pair(this.first, this.second);

  factory Pair.symmetric(int value) {
    return Pair(value, value);
  }
}

// Good: extends a non-Object superclass whose constructor const-ness is unknown.
class Base {
  final int a;
  const Base(this.a);
}

class Derived extends Base {
  final int b;
  Derived(this.b) : super(0);
}

// Good: applies a mixin (mixin const-ness unknown).
mixin Logger {}

class Service with Logger {
  final int id;
  Service(this.id);
}

// Good: a final field with a non-const initializer.
class Clock {
  final DateTime stamp = DateTime.now();
  Clock();
}

How to configure

Set the severity of prefer-declaring-const-constructor in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "prefer-declaring-const-constructor": "error"
      }
    }
  }
}