In Dart, you can add an item to a Set using the add() method.
In this example,
numbers
containing some integer elements.numbers
to add a new integer element, such as 6
, to the set.numbers
contains the added element 6
.var numbers = {1, 2, 3, 4, 5};
numbers.add(6);
print('Numbers: $numbers');
Numbers: {1, 2, 3, 4, 5, 6}
In this example,
fruits
containing some string elements.fruits
to add a new string element, such as 'Orange'
, to the set.fruits
contains the added element 'Orange'
.var fruits = {'Apple', 'Banana', 'Cherry'};
fruits.add('Orange');
print('Fruits: $fruits');
Fruits: {Apple, Banana, Cherry, Orange}
In this tutorial, we learned How to add an Item to a Set in Dart language with well detailed examples.