Empty collection filled by an addAll on the next line

ID

dart.performance.prefer_spread_collections

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Idiom

Language

Dart

Description

Reports an empty List, Set or Map literal that the immediately following statement fills with addAll. Only the adjacent form is reported, so a collection filled inside an if or a loop - where a spread cannot replace the call - is left alone.

Rationale

Building a collection in two steps allocates it at its default capacity and then grows it as the copy proceeds, so the elements are moved more than once; a spread element gives the literal the whole source up front. The two-step form is also mutable by construction - the variable has to stay writable, and it exists in a half-built state for as long as the addAll calls run, which is where a missed call or a copy in the wrong order hides. A single literal with spreads has neither problem and can be const where its sources are.

The following code illustrates the pattern detected by this rule:

class Report {
  List<String> allLines(List<String> header, List<String> body) {
    // FLAGGED: Empty collection filled by an addAll on the next line
    final lines = <String>[];
    lines.addAll(header);
    lines.addAll(body);
    return lines;
  }
}

Remediation

Fold the copies into the literal with spread elements - final lines = […​header, …​body]; - and use …​? where a source may be null. When a source is conditional, prefer a collection if or for element inside the literal over reintroducing the mutable build-up.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved