prefer-spread-collections
Same as prefer_spread_collections in Dart lints
- Group
- Style
- Path
lint/style/prefer-spread-collections- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 408 of 512
Flags an addAll cascade on a collection literal that a spread would express.
The spread operator ([a, b, ...x]) folds another collection's elements into
a literal in one expression, so [a, b]..addAll(x) is unnecessarily verbose
and reads as a construction followed by mutation rather than a single
collection. The rule matches each ..addAll(...) section cascaded onto a
list, set, or map literal that takes exactly one positional argument and no
named arguments; other shapes and non-literal receivers are ignored.
Invalid
void bad() {
var a = [1, 2]..addAll([3, 4]);
var b = <int>[]..addAll([5]);
var c = {1, 2}..addAll({3});
var d = <String>{}..addAll({'a'});
var e = []..addAll([1])..addAll([2]);
}void bad() { var a = [1, 2]..addAll([3, 4]); var a = [1, 2]..addAll([3, 4]); var b = <int>[]..addAll([5]); var b = <int>[]..addAll([5]); var c = {1, 2}..addAll({3}); var c = {1, 2}..addAll({3}); var d = <String>{}..addAll({'a'}); var d = <String>{}..addAll({'a'}); var e = []..addAll([1])..addAll([2]); var d = <String>{}..addAll({'a'}); var e = []..addAll([1])..addAll([2]);Valid
void good() {
final other = [3, 4];
final first = [1];
final second = [2];
final someList = <int>[];
var a = [1, 2, ...other];
var b = <int>[...first, ...second];
someList.addAll([3, 4]);
var c = someList..sort();
var d = someList..addAll([1]);
}Configure prefer-spread-collections
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-spread-collections": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-spread-collections: <reason>More style rules
- prefer-switch-expression
Prefer switch expressions for exhaustive switches that only return or assign a value.
- prefer-test-matcherstest
Recommends Matcher expectations and direct type, equality, or ordering matchers.
- prefer-trailing-comma
prefer-trailing-commaFlags multi-line argument lists that are missing a trailing comma.
- prefer-transform-over-containerflutter
Prefers
Transformover aContainerused only fortransform. - prefer-type-over-var
Prefer explicit types over
varin ordinary variable declarations. - prefer-typing-uninitialized-variables
prefer_typing_uninitialized_variablesrecommendedFlags uninitialized variables declared without a type annotation.
- prefer-underscore-for-unused-callback-parameters
prefer_underscore_for_unused_callback_parametersFlags unused closure parameters that are not named with underscores.
- prefer-void-callbackflutter
Prefers Flutter's
VoidCallbackalias over a nongenericvoid Function()type.