prefer-collection-literals
Same as prefer_collection_literals in Dart lints
- Group
- Style
- Path
lint/style/prefer-collection-literals- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 322 of 512
Flags no-argument collection constructor calls that a literal would express.
The empty List(), Map(), Set(), LinkedHashMap(), and LinkedHashSet()
constructors are more verbose than the equivalent [] or {} literals, which
are the idiomatic way to build a fresh collection and read more clearly. The
rule matches the unnamed and .new constructors written as a plain call
(List(), Set.new()), an explicit new expression (new Map()), or with
type arguments (Map<K, V>()). It deliberately spares named constructors such
as List.filled or Map.of and any invocation carrying arguments, since
those have no literal form.
Two further facts withhold a diagnostic that would otherwise change semantics:
- Concrete context — a
LinkedHashMap()/LinkedHashSet()whose declared context type (a variable/field/top-level/return annotation) is that concrete type. A{}literal has static typeMap/Set, which is not assignable back toLinkedHashMap/LinkedHashSet, so the constructor is required. This check is purely syntactic and applies without a type index. Local functions and operators use their own return annotations, never their enclosing function's. - Shadowed or unresolved identity — a
List()/Map()/Set()/… whose resolved declaration is not the SDK collection, or whose library/import graph cannot establish that identity. A literal could construct a different type. Without an identity index, the rule remains syntactic and such a constructor keeps firing.
Everything else — including any var/unannotated context, and a concrete
constructor reached only through a nested argument position — keeps firing.
Invalid
import 'dart:collection';
void bad() {
var a = List();
var b = Map();
var c = Set();
var d = LinkedHashMap();
var e = LinkedHashSet();
var f = List<int>();
var g = new Set();
var h = new Map<String, int>();
}
// A `{}` literal has static type `Map`, which IS assignable to a declared `Map`
// context — the concrete-type exception does not apply, so the diagnostic keeps
// firing. Only `LinkedHashMap`/`LinkedHashSet` contexts suppress.
void nonConcreteContext() {
Map<int, int> m = LinkedHashMap();
var n = LinkedHashMap();
}void bad() { var a = List(); var a = List(); var b = Map(); var b = Map(); var c = Set(); var c = Set(); var d = LinkedHashMap(); var d = LinkedHashMap(); var e = LinkedHashSet(); var e = LinkedHashSet(); var f = List<int>(); var f = List<int>(); var g = new Set(); var g = new Set(); var h = new Map<String, int>();void nonConcreteContext() { Map<int, int> m = LinkedHashMap(); Map<int, int> m = LinkedHashMap(); var n = LinkedHashMap();Valid
import 'dart:collection';
void good() {
var a = [];
var b = {};
var c = <int>[];
var d = <String, int>{};
var e = List.filled(3, 0);
var f = Map.from(<int, int>{});
var g = Set.of([1, 2, 3]);
var h = List.generate(3, (i) => i);
}
// A `{}`/`{}` literal has static type `Map`/`Set`, which is not assignable back
// to the concrete `LinkedHashMap`/`LinkedHashSet`, so these constructors are
// REQUIRED by their declared context type and the diagnostic is SUPPRESSED. This
// holds for local variables, fields, and return types.
// (This suppression is syntactic and does not need a type index, but the file as
// a whole exercises the user-shadow suppression below, which does.)
LinkedHashMap<int, int> makeMap() => LinkedHashMap();
class Holder {
LinkedHashSet<int> data = LinkedHashSet();
LinkedHashMap<int, int> build() {
LinkedHashMap<int, int> local = LinkedHashMap();
return local;
}
}
// A user-declared `Bag` named `Set` shadows core `Set`. Its `type_kind` is
// `Some` (user declarations carry one; core names do not), so `Set()` builds the
// user's type and a `{}` literal would build the wrong one — SUPPRESSED.
// (Requires the corpus harness to attach a TypeIndex for this rule; without one
// the name is Unknown and this line would fire.)
class Set {}
void shadowed() {
var s = Set();
}Configure prefer-collection-literals
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-collection-literals": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-collection-literals: <reason>More style rules
- prefer-commenting-analyzer-ignores
Require explanations for analyzer
ignore:andignore_for_file:comments. - prefer-const-constructors
prefer_const_constructorsPrefers
constwhen invoking aconstconstructor with constant arguments. - prefer-const-constructors-in-immutables
prefer_const_constructors_in_immutablesflutterPrefers
constconstructors in immutable classes and extension types. - prefer-const-declarations
prefer_const_declarationsrecommendedFlags
finaldeclarations whose initializers are all constant expressions. - prefer-const-literals-to-create-immutables
prefer_const_literals_to_create_immutablesPrefers
constlist, set, and map literals as arguments to constructors of@immutableclasses. - prefer-constrained-box-over-containerflutter
Prefers
ConstrainedBoxover aContainerused only forconstraints. - prefer-constructors-over-static-methods
prefer_constructors_over_static_methodsFlags static methods that create and return an instance of their own class.
- prefer-containerflutter
Suggests collapsing compatible nested layout widgets into one
Container.