FlutterDocs
Home › Guides › How to Check Key Index or Value Exists on Array List in Flutter/Dart

How to Check Key Index or Value Exists on Array List in Flutter/Dart

Restored from an archived FlutterDocs resource. Code examples and APIs may refer to earlier Flutter/Dart SDK versions.

In this example, you will learn how to check the Key index or value exists or not in Array List in Flutter or Dart. See the example below, we have shown an example to check key index or value in Array List.

How to Check if Value exists in Array List:

List<String> countries = ["Nepal", "India", "Pakistan", "USA", "Canada", "China"];
if(countries.contains("Nepal")){
      //there is element
}else{
      //there is no element
} 

You can use array.contains(value) to check if a value exists in the array List or not. It will return TRUE if the value is there and FALSE if the value is not there.

How to Check if Key Index exists in Array List:

List<String> countries = ["Nepal", "India", "Pakistan", "USA", "Canada", "China"];

if(countries.asMap().containsKey(4)){
    //if there is key, 
}else{
    //if there is not key
}

You can use array.asMap().containsKey(index)  to check if the Key index exists or not in Array List.

In this way, you can check if Array List has a Key index or value or not in Dart or Flutter.