dart

What is the best way to fillter null from a list in dart language


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

Solution

  • You can use the List's removeWhere method like so:

    List a = [null, 2, null];
    a.removeWhere((value) => value == null);
    print(a); // prints [2]