Dart spread operator
1 min read

Dart spread operator

Allows an iterable, such as an array expression or string, to be expanded in places where zero or more elements are expected.

Dart supports the spread operator, which allows to insert multiple elements into a collection.

For instance, from the cascade I described yesterday, this

return someVariable.toList()
    ..add(anotherObject)
    ..addAll(anotherListOfObjects);

becomes

return [
    ...someVariable.toList(),
    anotherObject,
    ...anotherListOfObjects
];

Even more concise and easier to read.

Wanna stay up-to-date with Flutter and Dart?

Subscribe to get a weekly email with the best articles about Flutter and Dart.

    We won't send you spam. Unsubscribe at any time.