Home > Enterprise >  flutter how to remove specific values inside List
flutter how to remove specific values inside List

Time:09-29

I have an List like this

List<MapEntry> _list = [
    MapEntry('a': []), 
    MapEntry('b': [Instance, Instance]), 
    MapEntry('c': [Instance]), 
    MapEntry('d': []), 
    MapEntry('e': [Instance, Instance]),
    MapEntry('f': []),
]

I need to make the

List<MapEntry> = [
        MapEntry('b': [Instance, Instance]), 
        MapEntry('c': [Instance]),
        MapEntry('e': [Instance, Instance]),
]

i was keep trying to use map methods but wasn't able to remove empty arrays inside the MapEntries value

what should I have to try?

CodePudding user response:

how about this:

void main() {
  List<MapEntry> _list = [
    MapEntry('a', []),
    MapEntry('b', ['Instance', 'Instance']),
    MapEntry('c', ['Instance']),
    MapEntry('d', []),
    MapEntry('e', ['Instance', 'Instance']),
    MapEntry('f', [])
  ];
  
  final ist = _list.where((e)=>(e.value as List).length>0).toList();
  
  print(ist);
// result:
// [MapEntry(b: [Instance, Instance]), MapEntry(c: [Instance]), MapEntry(e: [Instance, Instance])]
}
  • Related