{

Dart| Flutter How to check element exists in Array


How to check sublist exists in a list of  Dart and Flutter

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.

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.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart