132

I am trying to check condition

if (value in List) {
  exist
} else { 
  not exist
}

but nothing to help anyone having an idea then please share.

My List = _quantityController[];

itemId is integer

i want to check my item id exists in my list array or not...Thanks!

Hadrien Lejard
  • 5,644
  • 2
  • 21
  • 18
Rahul Mishra
  • 4,263
  • 7
  • 32
  • 53

10 Answers10

259
list.contains(x);

Contains method

Zroq
  • 8,002
  • 3
  • 26
  • 37
45

Above are the correct answers to the current question. But if someone like me is here to check value inside List of Class object then here is the answer.

class DownloadedFile {
 String Url;
 String location;
}

List of DownloadedFile

List<DownloadedFile> listOfDownloadedFile = List();
listOfDownloadedFile.add(...);

Now check if a specific value is inside this list

var contain = listOfDownloadedFile.where((element) => element.Url == "your URL link");
if (contain.isEmpty)
   //value not exists
else
  //value exists

There maybe better way/approach. If someone know, then let me know. :)

Abdullah Khan
  • 1,365
  • 15
  • 15
29

Checking array of class objects

Better than Abdullah Khan's approach is to use any instead of where because where makes the array to be scanned completely. Any stops when it finds one.

class DownloadedFile {
 String Url;
 String location;
}

List<DownloadedFile> files = [];
bool exists = files.any((file) => file.Url == "<some_url>");
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
23
List<int> values = [1, 2, 3, 4];
values.contains(1); // true
values.contains(99); // false   

Method - Contains of Iterable does exactly what you need. See the example above.

Kanso Code
  • 7,479
  • 5
  • 34
  • 49
9

If you are using custom class then make sure to override ==() method, so that dart can compare two objects.

Use this to check if a data is inside list:

mylist.contains(data)
Rituraj Shakti
  • 425
  • 6
  • 4
7

This was my case I have a list like this and I was looking for specific UUID within the List

 // UUID that I am looking for
 String lookingForUUID = "111111-9084-4869-b9ac-b28f705ea53b"


 // my list of comments
 "comments": [
            {
                "uuid": "111111-9084-4869-b9ac-b28f705ea53b",
                "comment": "comment"
            },
            {
                "uuid": "222222-9084-4869-b9ac-b28f705ea53b",
                "comment": "like"
            }
 ]

And this is how I iterate within the list

// This is how I iterate
var contain = someDataModel.comments.where((element) => element['uuid'] == lookingForUUID);
      if (contain.isEmpty){
        _isILike = false;
      } else {
        _isILike = true;
      }

That way I get the lookingForUUID in

Hope will help for someone

eko
  • 532
  • 1
  • 5
  • 12
5

Here's a complete example

void main() {
  List<String> fruits = <String>['Apple', 'Banana', 'Mango'];

  bool isPresent(String fruitName) {
    return fruits.contains(fruitName);
  }

  print(isPresent('Apple')); // true
  print(isPresent('Orange')); // false
}
Abhishek Kumar
  • 738
  • 1
  • 9
  • 17
  • 2
    What do you get when your answer only uses existing answers content? You won't get votes if you simply refactor a code. – iDecode May 11 '20 at 17:46
2

To check if a particular element in the Map contains a particular value:

if (myMap.any((item) => item.myKeyName == whatever)) {
  ...
} else {
  ...
}
Ehab Reda
  • 555
  • 5
  • 9
1

A solution other answers didn't mention: indexOf.

List<int> values = [2, 3, 4, 5, 6, 7];
print(values.indexOf(5) >= 0); // true, 5 is in values
print(values.indexOf(1) >= 0); // false, 1 is not in values

It also lets you search after an index. With contains, one would do:

print(values.sublist(3).contains(6)); // true, 6 is after index 3 in values
print(values.sublist(3).contains(2)); // false, 2 is not after index 3 in values

With indexOf:

print(values.indexOf(6, 3) >= 0); // true, 6 is after index 3 in values
print(values.indexOf(2, 3) >= 0); // false, 2 is not after index 3 in values
enzo
  • 9,861
  • 3
  • 15
  • 38
-3
List<int> list1 = [1, 2, 2, 3];
List<int> list2 = [3, 2, 2, 1];

bool isEqual = list1.toSet().containsAll(list2) && list2.toSet().containsAll(list1);
print(isEqual);

This should show true.

James Risner
  • 5,451
  • 11
  • 25
  • 47
Red Baby
  • 1
  • 2
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Mar 08 '23 at 06:57
  • The question is about a single value and an array or list. Your post is about two lists and does not seem to answer *this* question. – greybeard Mar 12 '23 at 14:20