How to delete a list inside the list in dart?

Viewed 44

I want to delete the list inside the list of the Map. The map contains key is dynamic and the value is List. The List is composed also of sub list and I want to delete some of the sub lists.


void main() {
  List list = [1,2];
  Map<dynamic, List> events = {1: list,
                                  2: [['a1','a2'],['a3','a4','a5'],'y'],
                                  'A': []};
  print(events);
  events[2]?.remove((item) => item == 'y' );//deleting only the 'y'
  print(events[1]);     //correct print
  print(events[2]);     //correct print
  print(events['A']);   //correct print
  print('above is correct--------below is error');
  
  events[2]?.remove((item) => item== ['a1','a2']);//trying to DELETE the ['a1',a2]
  print(events[2]); //error, ['a1','a2'] not DELETED :(
}

2 Answers

Try use removeAt(Index):

myList.removeAt(idx);
void main() {
  List list = [1,2];
  Map<dynamic, List> events = {1: list,
                                  2: [['a1','a2'],['a3','a4','a5'],'y'],
                                  'A': []};
  print(events);
  events[2]?.remove((item) => item == 'y' );//deleting only the 'y'
  print(events[1]);     //correct print
  print(events[2]);     //correct print
  print(events['A']);   //correct print
  print('above is correct--------below is error');
  
  events[2].removeAt(0);
  events[2].removeAt(1);
}
Related