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.

Subscribe to a curated newsletter

Receive an email every week with curated content about Dart and Flutter.

See previous issues of the newsletter.