prefer_spread_collections
Use spread collections when possible.
This rule is available as of Dart 2.3.
Rule sets: recommended, flutter
This rule has a quick fix available.
Details
#Use spread collections when possible.
Collection literals are excellent when you want to create a new collection out of individual items. But, when existing items are already stored in another collection, spread collection syntax leads to simpler code.
BAD:
dart
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: ListView(
children: [
Tab2Header(),
]..addAll(buildTab2Conversation()),
),
);
}
dart
var ints = [1, 2, 3];
print(['a']..addAll(ints.map((i) => i.toString()))..addAll(['c']));
dart
var things;
var l = ['a']..addAll(things ?? const []);
GOOD:
dart
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: ListView(
children: [
Tab2Header(),
...buildTab2Conversation(),
],
),
);
}
dart
var ints = [1, 2, 3];
print(['a', ...ints.map((i) => i.toString()), 'c');
dart
var things;
var l = ['a', ...?things];
Usage
#To enable the prefer_spread_collections
rule,
add prefer_spread_collections
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- prefer_spread_collections
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。