Home > Back-end >  How to join maps from List of Maps in Flutter?
How to join maps from List of Maps in Flutter?

Time:11-09

I have this List:

var listMap = [
        {
          "type": "Spouse",
          "name": "Mother",
          "age": 50
        },
        {
          "type": "Family",
          "name": "Lili",
          "age": 16
        },

];

var finalResult = [ "type": "Spouse", "name": "Mother", "age": 50, "type": "Family", "name": "Lili", "age": 16, ];

How can i achieve finalResult?

CodePudding user response:

 List<Map<String,dynamic>> listMap = [
        {
          "type": "Spouse",
          "name": "Mother",
          "age": 50
        },
        {
          "type": "Family",
          "name": "Lili",
          "age": 16
        },

];

try to loop them and add them 1 by 1

    for(var x in listMap){
          for(var y in x.entries){
            newList.add("${jsonEncode(y.key)}:${jsonEncode(y.value)}");
          }
       }
      // or use print
    log(newList.toList().toString());

result

["type":"Spouse", "name":"Mother", "age":50, "type":"Family", "name":"Lili", "age":16]

you can try this to dart pad.

  • Related