Home > database >  How to copy specific data from list in flutter
How to copy specific data from list in flutter

Time:04-19

I have a list of response from api which contains data with same dates , how can i sort the data with same date and create a new list with listing the data with same dates

CodePudding user response:

To get a copy for the list

list2 = List.from(list)

You can use sort method to set the list in place.

list2.sort((a, b) => a['date'].compareTo(b['date']);

CodePudding user response:

Try this You will get data according to your date.

 List tasks =  [ {
  "project_id": 7,
  "project_name": "ERP -Mobile App",
  "task_id": 42,
  "task_name": "ui design",
  "description": "ui design",
  "date": "2022-04-12",
  "hours": 5.0
},
  {
    "project_id": 8,
    "project_name": "ERP -Mobile App",
    "task_id": 42,
    "task_name": "ui design",
    "description": "ui design",
    "date": "2022-04-14",
    "hours": 5.0
  },
  {
    "project_id": 9,
    "project_name": "ERP -Mobile App",
    "task_id": 42,
    "task_name": "ui design",
    "description": "ui design",
    "date": "2022-04-13",
    "hours": 5.0
  },
  {
    "project_id": 11,
    "project_name": "ERP -Mobile App",
    "task_id": 42,
    "task_name": "ui design",
    "description": "ui design",
    "date": "2022-04-13",
    "hours": 5.0
  },
  {
    "project_id": 12,
    "project_name": "ERP -Mobile App",
    "task_id": 42,
    "task_name": "ui design",
    "description": "ui design",
    "date": "2022-04-13",
    "hours": 5.0
  },
  {
    "project_id": 10,
    "project_name": "ERP -Mobile App",
    "task_id": 42,
    "task_name": "ui design",
    "description": "ui design",
    "date": "2022-04-12",
    "hours": 5.0
  }];

Map finalList = {};

final ids = tasks.map((e) => e['date']).toSet();
ids.forEach((element) {

  finalList[element] = tasks.where((x) => x['date'] == element).toList();
});

print(finalList);
  • Related