What is the list.contains() method in Dart?

Overview

The contains() method is used to check if an element occurs in a list. This method takes one parameter and returns a boolean value indicating whether or not that item is found in the list.

Syntax

list_name.contains(searchItem);

list_name is the name of the list to be searched.

Parameters

The searchItem represents the item to be found.

Return value

The method contains() returns true if the item is found, otherwise it returns false.

Code

The following example shows how to use the contains() method. Click on the “Run” button to execute the code.

void main(){
// Creating list
var myList = [20, 15, 37, 23, 60, 50, 20];
// Item to be found
int searchItem = 23;
// stores boolean in variable found
bool found = myList.contains(searchItem);
if (found) {
print("$searchItem is present in the list");
} else {
print("$searchItem is not present in the list");
}
}

Free Resources