Dart – Check if List Contains a Given Element

Use the Dart List.contains() method to check whether a list has a specific element. The method returns true when an equal element is found and false when no match exists.

For most exact-value checks, contains() is the clearest option. When the match depends on a condition, such as a string prefix or an object property, use any(). A manual loop is useful when you need additional control while searching.

Dart List.contains() Syntax and Return Value

The contains() method accepts the element to search for and returns a Boolean value.

</>
Copy
bool result = list.contains(element);

Dart checks the list elements in order and stops when it finds the first element equal to the requested value. Equality is normally determined with the == operator.

Check if a Dart List Contains a Number with contains()

In this example, the list contains 84 but does not contain 77.

Dart Program

</>
Copy
void main(){
	var myList = [24, 56, 84, 92];
	
	var element = 84;
	
	if(myList.contains(element)){
		print('$element is present in the list $myList');
	} else {
		print('$element is not present in the list $myList');
	}
	
	element = 77;
	
	if(myList.contains(element)){
		print('$element is present in the list $myList');
	} else {
		print('$element is not present in the list $myList');
	}
}

Output

84 is present in the list [24, 56, 84, 92]
77 is not present in the list [24, 56, 84, 92]

Because 84 is present, myList.contains(84) returns true. Because 77 is absent, myList.contains(77) returns false.

Check if a Dart List Contains a String

String matching with contains() is case-sensitive. For example, 'dart' and 'Dart' are different values.

</>
Copy
void main() {
  final languages = <String>['Dart', 'Kotlin', 'Swift'];

  print(languages.contains('Dart'));
  print(languages.contains('dart'));
}
true
false

For a case-insensitive search, normalize both the list value and the search value with toLowerCase() or toUpperCase().

</>
Copy
void main() {
  final languages = <String>['Dart', 'Kotlin', 'Swift'];
  final searchValue = 'dart';

  final found = languages.any(
    (language) => language.toLowerCase() == searchValue.toLowerCase(),
  );

  print(found);
}
true

Check a Dart List with a For Loop

A manual loop can perform the same exact-value check. It is mainly useful when you need to run extra logic, record the matching index, or stop under a custom condition.

Dart Program

</>
Copy
void main(){
	var myList = [24, 56, "hello", "dart"];
	
	var element = "hello";
	
	var present = false;
	for(var i=0;i<myList.length;i++) {
		// you may have to check the equality operator
		if(element == myList[i]) {
			present=true;
			break;
		}
	}
	
	if(present){
		print('$element is present in the list $myList');
	} else {
		print('$element is not present in the list $myList');
	}
}

Output

hello is present in the list [24, 56, hello, dart]

For a straightforward membership check, prefer contains() because it expresses the intent directly and already stops at the first match.

Use any() When the Dart List Match Needs a Condition

contains() looks for an equal element. Use any() when an element should match a test instead. The following example checks whether any number is greater than 50.

</>
Copy
void main() {
  final numbers = <int>[12, 28, 64, 35];

  final hasNumberAbove50 = numbers.any((number) => number > 50);

  print(hasNumberAbove50);
}
true

Dart List.contains() with Custom Objects

For objects of your own class, contains() uses that class’s == operator. Without a custom equality implementation, two separate instances with the same field values are normally treated as different objects.

</>
Copy
class Product {
  final int id;
  final String name;

  const Product(this.id, this.name);

  @override
  bool operator ==(Object other) {
    return other is Product && other.id == id && other.name == name;
  }

  @override
  int get hashCode => Object.hash(id, name);
}

void main() {
  final products = <Product>[
    const Product(1, 'Keyboard'),
    const Product(2, 'Mouse'),
  ];

  print(products.contains(const Product(2, 'Mouse')));
}
true

If you only need to compare one property, any() can be simpler than creating a complete object for the search.

</>
Copy
final hasProductId2 = products.any((product) => product.id == 2);

Time Cost of Searching a Dart List

A list search may inspect every element before returning false, so the worst-case time cost grows linearly with the list length. For occasional checks, List.contains() is appropriate. If an application repeatedly performs membership checks on a large collection of unique values, a Set may be a better data structure.

Dart List contains FAQs

Does Dart List.contains() return an index?

No. It returns only true or false. Use indexOf() when you need the position of an equal element.

Can List.contains() find null in a Dart list?

Yes, when the list’s element type permits null. For example, <String?>['Dart', null].contains(null) returns true.

Is Dart List.contains() case-sensitive for strings?

Yes. String equality is case-sensitive, so 'Dart' does not equal 'dart'. Use any() with normalized strings for a case-insensitive check.

What is the difference between contains() and any() in Dart?

contains() checks whether an equal value exists. any() checks whether at least one element satisfies a Boolean condition.

Dart List Membership Summary

Use List.contains(element) for an exact membership check, any() for condition-based matching, and a loop when the search needs additional processing. In this Dart Tutorial, we checked numbers, strings, nullable values, and custom objects in a Dart list.