prefer-declaring-const-constructor

Same as prefer_declaring_const_constructor in Pyramid Lint

Path
lint/performance/prefer-declaring-const-constructor
Default
Off, opt in
Origin
Pyramid Lint
Domain
flutter
Rule
345 of 512
flutter

Flags a generative constructor that is legally eligible to be const.

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();
}

class Computed {
  final int value;

  Computed(int base) : value = base + 1;
}

class Asserted {
  final int value;

  Asserted(this.value) : assert(value >= 0);
}

class Redirected {
  final int value;
  const Redirected._(this.value);
  Redirected(int value) : this._(value);
}
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();
Constructor could be declared as `const`.
53
54 Computed(int base) : value = base + 1;
Constructor could be declared as `const`.
59
60 Asserted(this.value) : assert(value >= 0);
Constructor could be declared as `const`.
65 const Redirected._(this.value);
66 Redirected(int value) : this._(value);

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();
}

class BodyForm {
  final int value;
  BodyForm(this.value) {}
}

Configure prefer-declaring-const-constructor

Set its severity in falcon.json. See the configuration guide.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/performance/prefer-declaring-const-constructor: <reason>
  • prefer-foreachprefer_foreach

    Prefers forEach to a for-in loop that only passes each element to a function.

  • prefer-return-await

    Await returned SDK Futures inside a try body protected by a catch clause.

  • prefer-single-setstateflutter

    Merge consecutive Flutter State.setState statements in the same block.

  • prefer-use-callbackflutter_hooks

    Prefer useCallback for synchronous memoized function literals.

  • prefer-use-memoflutter_hooks

    Prefer memoizing freshly allocated collection values used as hook keys.

  • prefer-using-list-viewflutter

    Suggests considering ListView for a SingleChildScrollView containing a plain Flutter Column with at least two unconditional children in a list literal.

  • sized-box-for-whitespacesized_box_for_whitespacerecommendedflutter

    Flags a Container used only to add fixed width/height whitespace.

  • unnecessary-to-list-in-spreadsunnecessary_to_list_in_spreadsrecommended

    Flags a .toList() call on the operand of a spread, e.g.