Home > Enterprise >  how to remove a field from a dart list without affecting main list
how to remove a field from a dart list without affecting main list

Time:11-02

I want to remove the id field from datacopy list. but when I do it, also data list is updated. I want to remove the id field only from datacopy list. how to do that?

void main() {
List<Map<String, dynamic>> data = [
  {
    'id': '1',
    'domain': '00extreme.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00 ] [ extreme ]'
  },
  {
    'id': '2',
    'domain': '00hrs.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00 ] [ hrs ]'
  },
  {
    'id': '3',
    'domain': '00k3.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00 ] [ k ] [ 3 ]'
  },
  {
    'id': '4',
    'domain': '00I.net',
    'TDL': 'net',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00I.net ]'
  },
  {
    'id': '5',
    'domain': '02text.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 02 ] [ text ]'
  },
  {
    'id': '6',
    'domain': '0zzy.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 0 ] [ zz ] [ y ]'
  },
  {
    'id': '7',
    'domain': '100facial.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'Yes',
    'keywords': '[ 100 ] [ facial ]'
  },
   {
    'id': '8',
    'domain': '1000fotosgratis.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 1000 ] [ fotos ] [ gratis ]'
  },
   {
    'id': '9',
    'domain': '1000petstores.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 1000 ] [ pet ] [ stores ]'
  },
   {
    'id': '10',
    'domain': '1000toys.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 100 ] [ toys ]'
  },

];
  List<Map<String, dynamic>> datacopy =[];
  datacopy = [...data];

  removeItem(String key) {
   datacopy.map((element) {
      var m = element;
      m.remove(key);
      return m;
    }).toList();
    
  }
  removeItem('id');
  print(data);
  print(datacopy);
}

I want to remove the id field from datacopy list. but when I do it, also data list is updated. I want to remove the id field only from datacopy list. how to do that?

CodePudding user response:

I think the inner map also passing as reference. You can create new map.

  datacopy.addAll(data.map((e) => Map.from(e)));

  for (final item in datacopy) {
    item.remove("id");
  }
  print(data);
  print("NA\n");
  print(datacopy);

CodePudding user response:

Try the following code:

void main() {
List<Map<String, dynamic>> data = [
  {
    'id': '1',
    'domain': '00extreme.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00 ] [ extreme ]'
  },
  {
    'id': '2',
    'domain': '00hrs.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00 ] [ hrs ]'
  },
  {
    'id': '3',
    'domain': '00k3.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00 ] [ k ] [ 3 ]'
  },
  {
    'id': '4',
    'domain': '00I.net',
    'TDL': 'net',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 00I.net ]'
  },
  {
    'id': '5',
    'domain': '02text.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 02 ] [ text ]'
  },
  {
    'id': '6',
    'domain': '0zzy.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 0 ] [ zz ] [ y ]'
  },
  {
    'id': '7',
    'domain': '100facial.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'Yes',
    'keywords': '[ 100 ] [ facial ]'
  },
   {
    'id': '8',
    'domain': '1000fotosgratis.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 1000 ] [ fotos ] [ gratis ]'
  },
   {
    'id': '9',
    'domain': '1000petstores.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 1000 ] [ pet ] [ stores ]'
  },
   {
    'id': '10',
    'domain': '1000toys.com',
    'TDL': 'com',
    'status': 'parked',
    'adult': 'No',
    'keywords': '[ 100 ] [ toys ]'
  },

];
  List<Map<String, dynamic>> datacopy =[];
  addData() {
   datacopy.addAll(data);
  }
  addData();

  removeItem(String key) {
   datacopy.map((element) {
      var m = element;
      m.remove(key);
      return m;
    }).toList();
    
  }
  removeItem('id');
  print(data);
  print(datacopy);
}
  • Related