Contents

prefer_const_literals_to_create_immutables

Contents

Prefer const literals as parameters of constructors on @immutable classes.

This rule is available as of Dart 2.0.

This rule has a quick fix available.

Details

#

PREFER using const for instantiating list, map and set literals used as parameters in immutable class instantiations.

BAD:

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

A a1 = new A([1]);
A a2 = new A({});

GOOD:

dart
A a1 = new A(const [1]);
A a2 = new A(const {});

Usage

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - prefer_const_literals_to_create_immutables