To create an empty set in Dart, you can use the Set data structure without specifying any initial elements.
In this example,
emptySet
of type Set<int>
to indicate that it will hold integer values.emptySet
by directly assigning it the Set
constructor without passing any initial elements.emptySet
.void main() {
// Declare an empty set
Set<int> emptySet = Set<int>();
// emptySet is now ready to be used as an empty set
// Example usage:
// Add elements to the set
emptySet.add(5);
// Remove elements from the set
emptySet.remove(5);
// Check if an element is present in the set
bool containsElement = emptySet.contains(5);
print('Is element 5 present in the set: $containsElement');
}
Is element 5 present in the set: false
In this example,
anotherEmptySet
of type Set<String>
to indicate that it will hold string values.anotherEmptySet
by directly assigning it an empty set literal {}
, representing an empty set in Dart.anotherEmptySet
using set operations such as adding elements, removing elements, and checking for membership.void main() {
// Declare an empty set using set literal
Set<String> anotherEmptySet = {};
// anotherEmptySet is now ready to be used as an empty set
// Example usage:
// Add elements to the set
anotherEmptySet.add('apple');
// Remove elements from the set
anotherEmptySet.remove('apple');
// Check if an element is present in the set
bool containsElement = anotherEmptySet.contains('apple');
print('Is element 'apple' present in the set: $containsElement');
}
Is element 'apple' present in the set: false
In this tutorial, we learned How to create an Empty Set in Dart language with well detailed examples.