Dart| Flutter How to check element exists in Array
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.
How do Check values exist in an Array or List of dart/flutters?
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
}
- use indexOf
void main() {
List<String> array = ["one", "two", "three"];
print(array.indexOf("one") >= 0); // true,
print(array.indexOf("four") >= 0); // false,
}
Conclusion
To summarize, Learn how to check the sublist contains all elements in a list dart and flutter.