Saeed Rahmatolahi
Saeed Rahmatolahi

Reputation: 1338

how to get values of the some keys in Array of Dictionary in Dart Flutter?

I have an Array of the Dictionary which the Dictionary contains many key values , I just want one keys values be in an array I do not want to loop Throw that with for loop I want some mapping but I do not know why it will sho0w me Error

So here is an Example

static final myDictionary = [
{
  "id": 1,
  "name": "Alex",
  "field_of_work": "HR",
  "description": ""
},
{
  "id": 2,
  "name": "Jack",
  "field_of_work": "CEO",
  "description": ""
},
{
  "id": 3,
  "name": "Eric",
  "field_of_work": "Worker",
  "description": ""
},
{
  "id": 4,
  "name": "Sam",
  "field_of_work": "Computer",
  "description": ""
},
{"id": 5, "name": "Michael", "field_of_work": "Others", "description": ""}
];

and I want my list be some thing like this

['Alex','Jack','Eric','Sam','Michael']

so here is the Code Which I wrote but Does not work (actually doe not compile)

myDictionary.asMap().values['name'].toList()

and here is the Error

The operator '[]' isn't defined for the type 'Iterable<Map<String, Object>>'.

Try defining the operator '[]'

Upvotes: 0

Views: 4158

Answers (1)

Moaid ALRazhy
Moaid ALRazhy

Reputation: 1744

try it like this

 final List<String> namesList = myDictionary.map((e) => e["name"].toString()).toList();

Upvotes: 3

Related Questions