prefer-declaring-const-constructor
Same as prefer_declaring_const_constructor in Pyramid Lint
- Group
- Performance
- Path
lint/performance/prefer-declaring-const-constructor- Default
- Off, opt in
- Origin
- Pyramid Lint
- Domain
- flutter
- Rule
- 345 of 512
Flags a generative constructor that is legally eligible to be const.
Invalid
// 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);
} Point(this.x, this.y); Color(this.red, this.green, this.blue); Coordinate(this.latitude, this.longitude, this.label); Size(this.width, this.height); Duration(this.seconds, this.milliseconds); Flags(); Computed(int base) : value = base + 1; Asserted(this.value) : assert(value >= 0); const Redirected._(this.value); Redirected(int value) : this._(value);Valid
// 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.
{
"linter": {
"rules": {
"performance": {
"prefer-declaring-const-constructor": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/prefer-declaring-const-constructor: <reason>More performance rules
- prefer-foreach
prefer_foreachPrefers
forEachto afor-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
useCallbackfor synchronous memoized function literals. - prefer-use-memoflutter_hooks
Prefer memoizing freshly allocated collection values used as hook keys.
- prefer-using-list-viewflutter
Suggests considering
ListViewfor aSingleChildScrollViewcontaining a plain Flutter Column with at least two unconditional children in a list literal. - sized-box-for-whitespace
sized_box_for_whitespacerecommendedflutterFlags a
Containerused only to add fixedwidth/heightwhitespace. - unnecessary-to-list-in-spreads
unnecessary_to_list_in_spreadsrecommendedFlags a
.toList()call on the operand of a spread, e.g.