如何在 Dart Flutter 中获取字典数组中某些键的值?
how to get values of the some keys in Array of Dictionary in Dart Flutter?
我有一个字典数组,其中字典包含许多键值,我只希望一个键值在一个数组中我不想循环使用 for 循环抛出它我想要一些映射但我不知道为什么它会向我显示错误
所以这是一个例子
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": ""}
];
我希望我的列表是这样的
['Alex','Jack','Eric','Sam','Michael']
所以这是我写的代码但不起作用(实际上不编译)
myDictionary.asMap().values['name'].toList()
这是错误
The operator '[]' isn't defined for the type 'Iterable<Map<String, Object>>'.
尝试定义运算符“[]”
这样试试
final List<String> namesList = myDictionary.map((e) => e["name"].toString()).toList();
我有一个字典数组,其中字典包含许多键值,我只希望一个键值在一个数组中我不想循环使用 for 循环抛出它我想要一些映射但我不知道为什么它会向我显示错误
所以这是一个例子
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": ""}
];
我希望我的列表是这样的
['Alex','Jack','Eric','Sam','Michael']
所以这是我写的代码但不起作用(实际上不编译)
myDictionary.asMap().values['name'].toList()
这是错误
The operator '[]' isn't defined for the type 'Iterable<Map<String, Object>>'.
尝试定义运算符“[]”
这样试试
final List<String> namesList = myDictionary.map((e) => e["name"].toString()).toList();