Dart – Spread Operator (…)
Last Updated :
31 Jan, 2022
In Dart, Spread Operator (…) and Null-aware Spread Operator (…?) are used for inserting multiple elements in a collection like Lists, Maps, etc.
Syntaxes:
...Data_structure
- Null-aware Spread operator
...?Data_structure
Example 1: Using spread operators with List.
Dart
void main() {
List? l1 = [ "Geeks" , "For" , "Geeks" ];
List? l2=[ "Wow" ,...l1, "is" , "amazing" ];
print(l2);
}
|
Output :
[Wow, Geeks, For, Geeks, is, amazing]
Example 2: Using Spread operator with Map.
Dart
void main() {
Map? m1 = { "name" : "John" , "age" :21};
Map? m2={ "roll no" :45, "class" :12,...m1};
print(m2);
}
|
Output :
{roll no: 45, class: 12, name: John, age: 21}
Example 3: Using spread operator with Sets.
Dart
void main() {
Set< int > s1 = {5, 4, 3};
Set< int > s2 = {3, 2, 1};
Set< int > result = {...s1, ...s2};
print(result);
}
|
Output:
{5,4,3,2,1}
Share your thoughts in the comments
Please Login to comment...