目錄
目錄

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

This rule is available as of Dart 2.0.0.

Rule sets: flutter

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:

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

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

GOOD:

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:

linter:
  rules:
    - prefer_const_literals_to_create_immutables