Reputation: 4583
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!
Upvotes: 142
Views: 264393
Reputation: 1
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.
Upvotes: -3
Reputation: 722
To check if a particular element in the Map contains a particular value:
if (myMap.any((item) => item.myKeyName == whatever)) {
...
} else {
...
}
Upvotes: 4
Reputation: 493
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)
Upvotes: 10
Reputation: 11531
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
Upvotes: 2
Reputation: 672
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
Upvotes: 7
Reputation: 10632
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>");
Upvotes: 39
Reputation: 1481
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. :)
Upvotes: 48
Reputation: 832
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
}
Upvotes: 5
Reputation: 7630
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.
Upvotes: 24