In Dart, you can create a copy of a set by using the spread operator (...) to create a new set with the same elements as the original set.
In this example,
numbers
containing some integer elements.copiedNumbers
by using the spread operator (...) with numbers
.copiedNumbers
contains a copy of all elements from numbers
.void main() {
var numbers = {1, 2, 3, 4, 5};
var copiedNumbers = {...numbers};
print('Copied Numbers:');
copiedNumbers.forEach((number) => print(number));
}
Copied Numbers: 1 2 3 4 5
In this example,
fruits
containing some string elements.copiedFruits
by using the spread operator (...) with fruits
.copiedFruits
contains a copy of all elements from fruits
.void main() {
var fruits = {'Apple', 'Banana', 'Cherry', 'Date'};
var copiedFruits = {...fruits};
print('Copied Fruits:');
copiedFruits.forEach((fruit) => print(fruit));
}
Copied Fruits: Cherry Date Banana Apple
In this tutorial, we learned How to Copy a Set in Dart language with well detailed examples.