Check value in array exists Flutter dart
DartFlutterDart Problem Overview
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!
Dart Solutions
Solution 1 - Dart
list.contains(x);
Solution 2 - Dart
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. :)
Solution 3 - Dart
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.
Solution 4 - Dart
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>");
Solution 5 - Dart
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
}
Solution 6 - Dart
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
Solution 7 - Dart
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.
Solution 8 - Dart
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
Solution 9 - Dart
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)