简体   繁体   中英

how to get values of some values as type<String> which is inside a Map inside List in Dart/Flutter?

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.

在此处输入图像描述

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"]


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);
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM