dexdim
dexdim

Reputation: 245

Flutter : How to get an index from a list by searching from a value?

I have a list:

List user = [
{"name": "Alex", "id": "001"}, 
{"name": "Brandon", "id": "002"}, 
{"name": "Charlie", "id": "003"}, 
{"name": "Danny", "id": "004"},
];

and I have a variable with value

String a = "002";
int index;

My question is how to get an index value from a list of user while I do a search by passing a value from a ?

So I'll have an index = 1

Thank you :)

Upvotes: 5

Views: 17647

Answers (2)

Chinmay Mourya
Chinmay Mourya

Reputation: 814

If you have to search in respect to any value in Map then you can do the following.

int index = user.indexWhere((item) => item.containsValue(a));

Upvotes: 2

Alex Radzishevsky
Alex Radzishevsky

Reputation: 3768

 int index = user.indexWhere((item) => item["id"] == a);

Upvotes: 14

Related Questions