60
votes

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!

8
What kind of element do you have in you list ? can you update your question with more context ?Hadrien Lejard
I have list like List<int> _itemController = [16,18]; int itemId = 16; I want to check itemId exists in _quantityController or not?Rahul Mishra

8 Answers

144
votes
list.contains(x);

Contains method

17
votes

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

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

3
votes

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
}
2
votes

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

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

0
votes

Presence of a key in an array can be found using a method offered by flutter: containsKey

it can be used like this

list.containsKey(key_you_are_looking_for)

It's result is boolean.

0
votes

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