I am trying to extract the values which key equal Name in a key:value pair that i extracted from an API. I am using the following formula;
final List<String> namesList =list.map((e) => e["Name"].toString()).toList();
print(namesList);
I can extract the values of name keys, however it is extracted as JSArray. I couldn't extract them as string.
Result: [Name1, Name2] JSArray Should be: ["Name1", "Name2"] List
Thank you.
CodePudding user response:
It seems that nameList element type is String.
The 'print' result only is shown remove double quotation marks.
Or you really want to make a list with double quotation marks.
void main() {
List list = [{'Code': 'ABC', 'Name': 'Name1', 'Type': '1'}, {'Code': 'DEF', 'Name': 'Name1', 'Type': '2'}];
final List<String> namesList =list.map<String>((e) => "\"${e["Name"]}\"").toList();
print(namesList);
print(namesList[0].runtimeType);
}
CodePudding user response:
Please refer to below code
void main() {
List list = [
{"code": "ABC", "Name": "\"Name1\"", "Type": "1"},
{"code": "DEF", "Name": "\"Name2\"", "Type": "2"}
];
final List<String> nameList = list.map((e) => e["Name"].toString()).toList();
print(nameList);
List list1 = [
{"code": "ABC", "Name": "Name1", "Type": "1"},
{"code": "DEF", "Name": "Name2", "Type": "2"}
];
final List<String> nameList1 =
list1.map((e) => '"${e["Name"].toString()}"').toList();
print(nameList1);
}
// Result
// ["Name1", "Name2"]