To access items in a set in Dart, you can use iteration or specific methods provided by the Set class.
In this example,
stringSet
containing strings.void main() {
Set<String> stringSet = {"Apple", "Banana", "Orange", "Grapes"};
for (var item in stringSet) {
print(item);
}
}
Apple Banana Orange Grapes
In this example,
numberSet
containing integers.contains
method to check if an item exists in the set.length
property to get the number of elements in the set.void main() {
Set<int> numberSet = {1, 2, 3, 4};
print("Number of elements: ${numberSet.length}");
print("Does set contain 3? ${numberSet.contains(3)}");
}
Number of elements: 4 Does set contain 3? true
In this tutorial, we learned How to Access Items in a Set in Dart language with well detailed examples.