THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial shows how to check if element exists in an array or list Dart or Flutter List
In dart, Array, and List store the same type of collections, Arrays are fixed, and List is dynamic.
Let’s see how to check if an element exists in an array or list.
dart list provides multiple ways to check the sub-list contains in the list of elements.
First Convert the list into set using the Set.of()
method
Check sub-list exists in Set using the Set.containsAll()
method
Here is an example code
void main() {
List<int> array = [1, 4, 5, 2, 8];
print(array.contains(2)); // true
print(array.contains(21)); // false
}
void main() {
List<String> array = ["one", "two", "three"];
print(array.contains("one"));// true
print(array.contains("four")); // false
}
void main() {
List<String> array = ["one", "two", "three"];
print(array.indexOf("one") >= 0); // true,
print(array.indexOf("four") >= 0); // false,
}
To summarize, Learn how to check the sublist contains all elements in a list dart and flutter.
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts