Looking for a dart equivalent of python filter
a = ['', None,4] [print(e) for e in filter(None,a)]
My code is too ugly:
List a = [null,2,null]; List b=new List(); for(var e in a){if(e==null) b.add(e);} for(var e in b){a.remove(e);} print(a);
removeWhere() function We can use contains() function in For Loop to remove empty, null, false, 0 values from a List.
Answer To remove elements where the id is null, you can use the removeWhere and check if the the current Map with the key id is null. myList. removeWhere((e) => e["id"] == null);
Dart allows returning null in functions with void return type but it also allow using return; without specifying any value. To have a consistent way you should not return null and only use an empty return.
in new dart 2.12 with sound null safety, the best way is:
List<int?> a = [null,2,null]; final List<int> b = a.whereType<int>().toList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With