Home > Software engineering >  Flutter : Lists map combine
Flutter : Lists map combine

Time:03-06

I made three lists, the first for free servers, the second for paid servers, and the third list includes both together. The problem is that when he adds the lists in the third list, he adds them in order In other words, he adds the whole first list above and the second list below I want a way to combine the two lists and arrange them according to name, number, or any other thing. The important thing is that they are not arranged according to the same list (the first is above and the second is below) enter image description here

CodePudding user response:

You can use sort method on 'allservs'. Here is a working sample that sorts combined list based on 'cnumber':

var servers = List.from([
  {
    'name': 'Singapore',
    'flag': 'assets/images/flags/singapore.png',
    'cnumber': 7,
    'isvip': false,
  },
  {
    'name': 'Singapore',
    'flag': 'assets/images/flags/singapore.png',
    'cnumber': 2,
    'isvip': false,
  },
  {
    'name': 'Singapore',
    'flag': 'assets/images/flags/singapore.png',
    'cnumber': 1,
    'isvip': false,
  },
]);

var newServers = List.from([
  {
    'name': 'Singapore',
    'flag': 'assets/images/flags/singapore.png',
    'cnumber': 7,
    'isvip': false,
  },
  {
    'name': 'Singapore',
    'flag': 'assets/images/flags/singapore.png',
    'cnumber': 12,
    'isvip': false,
  },
  {
    'name': 'Singapore',
    'flag': 'assets/images/flags/singapore.png',
    'cnumber': 3,
    'isvip': false,
  },
]);

get allServers => (List.from(newServers)   List.from(servers))
  ..sort((a, b) => (a['cnumber'] >= b['cnumber']) ? 1 : -1);

void main() {
  print(allServers);
}

CodePudding user response:

The simples way would be to do it like this:

 var allServers = [...servers, ...newsServers ];

For Example, you can see this sample in DartPad:

image

which would result in outputing:

[{name: Singapore}, {name1: Singapore1}]
  • Related