prefer_const_constructors_in_immutables

Stable
Flutter
Fix available

Prefer declaring const constructors on @immutable classes.

Details

#

PREFER declaring const constructors on @immutable classes.

If a class is immutable, it is usually a good idea to make its constructor a const constructor.

BAD:

dart
@immutable
class A {
  final a;
  A(this.a);
}

GOOD:

dart
@immutable
class A {
  final a;
  const A(this.a);
}

Enable

#

To enable the prefer_const_constructors_in_immutables rule, add prefer_const_constructors_in_immutables under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - prefer_const_constructors_in_immutables

If you're instead using the YAML map syntax to configure linter rules, add prefer_const_constructors_in_immutables: true under linter > rules:

analysis_options.yaml
yaml
linter:
  rules:
    prefer_const_constructors_in_immutables: true