Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check value in array exists Flutter dart

Tags:

flutter

dart

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!

like image 633
Rahul Mishra Avatar asked Sep 17 '18 13:09

Rahul Mishra


People also ask

How do you check a value is present in a list in Flutter?

Using List.contains( ) is the recommended method to check if a value is present in a list or not. This method takes the value to be searched as a parameter and returns true if the item found otherwise returns false.

How do you check if an element is present in a list in Dart?

The contains() method is used to check if an element occurs in a list.

How do you check if a string is in a list in Dart?

Check if Dart List contains Element using List.contains(element) returns true if the element is present in this list. Else, it returns false.


2 Answers

list.contains(x); 

Contains method

like image 86
Zroq Avatar answered Oct 04 '22 11:10

Zroq


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

like image 34
Abdullah Khan Avatar answered Oct 04 '22 11:10

Abdullah Khan