简体   繁体   中英

How to delete a list inside the list in dart?

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 :(
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM