143

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!

2
  • What kind of element do you have in you list ? can you update your question with more context ? Commented Sep 17, 2018 at 13:54
  • I have list like List<int> _itemController = [16,18]; int itemId = 16; I want to check itemId exists in _quantityController or not? Commented Sep 18, 2018 at 4:34

10 Answers 10

291
list.contains(x);

Contains method

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect and powerful method. Also works on detecting subStrings.
48

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. :)

3 Comments

Exactly what i was looking for.It works. Thanks Abdullah!
The contains should work, however I don't know why it was not working for me where my list is a list of objects. This method worked for me, many thanks.
if it exists returns the whole object. Can we just get the one we were looking for?
39

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>");

Comments

24
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.

Comments

10

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)

Comments

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

Comments

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
}

1 Comment

What do you get when your answer only uses existing answers content? You won't get votes if you simply refactor a code.
5

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

if (myMap.any((item) => item.myKeyName == whatever)) {
  ...
} else {
  ...
}

Comments

2

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

Comments

-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.

2 Comments

While this code may solve the question, including an explanation 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.