How to spread a list in dart

DartFlutter

Dart Problem Overview


In Javascript I would use a spread operator:

enter image description here

Now I have the same problem with Flutter:

 Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        MyHeader(),
        _buildListOfWidgetForBody(), // <- how to spread this <Widget>[] ????
        MyCustomFooter(),
      ],
    );
  }

Dart Solutions


Solution 1 - Dart

You can now do spreading from Dart 2.3

var a = [0,1,2,3,4];
var b = [6,7,8,9];
var c = [...a,5,...b];

print(c);  // prints: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Solution 2 - Dart

There is an issue to add this to future version of dart https://github.com/dart-lang/language/issues/47

but for now you can use sync* and yield*

Iterable<Widget> _buildChildren sync* {
  yield MyHeader();
  yield* _buildListOfWidgetForBody();
  yield MyCustomFooter();
}

EDIT: As of Dart 2.3 you can now do:

Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        MyHeader(),
        ..._buildListOfWidgetForBody(),
        MyCustomFooter(),
      ],
    );
  }

Solution 3 - Dart

Update - 20th April 2019

You can now use the spread operator since Dart 2.3 was released.

List<int> a = [0,1,2,3,4];
List<int> b = [6,7,8,9];
List<int> c = [...a,5,...b];

Solution 4 - Dart

FireStore use another field in model

authInstance
    .collection('fav_product')
    .add(
{
...product.toMap(),
'field':'value',
}
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTSRView Question on Stackoverflow
Solution 1 - DartikbenView Answer on Stackoverflow
Solution 2 - DartJordan DaviesView Answer on Stackoverflow
Solution 3 - DartMiguel RuivoView Answer on Stackoverflow
Solution 4 - DartMuhammad UmarView Answer on Stackoverflow