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
Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examples How to run only single test cases with Gradle build How to print dependency tree graph in Gradle projects How to perform git tasks with build script?Related posts