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

Time:11-30

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

enter image description here

Thank you.

CodePudding user response:

It seems that nameList element type is String.
The 'print' result only is shown remove double quotation marks.

enter image description here

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

enter image description here

CodePudding user response:

enter image description here

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


  • Related