Skip to content

Files

Latest commit

 

History

History
63 lines (49 loc) · 1.1 KB

prefer_spread_collections.md

File metadata and controls

63 lines (49 loc) · 1.1 KB

Pattern: Missing use of spread collection

Issue: -

Description

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.

Example of incorrect code:

Widget build(BuildContext context) {
 return CupertinoPageScaffold(
  child: ListView(
   children: [
    Tab2Header(),
   ]..addAll(buildTab2Conversation()),
  ),
 );
}
var ints = [1, 2, 3];
print(['a']..addAll(ints.map((i) => i.toString()))..addAll(['c']));
var things;
var l = ['a']..addAll(things ?? const []);

Example of correct code:

Widget build(BuildContext context) {
 return CupertinoPageScaffold(
  child: ListView(
   children: [
    Tab2Header(),
    ...buildTab2Conversation(),
   ],
  ),
 );
}
var ints = [1, 2, 3];
print(['a', ...ints.map((i) => i.toString()), 'c');
var things;
var l = ['a', ...?things];

Further Reading