Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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); 
like image 575
TastyCatFood Avatar asked Dec 29 '15 15:12

TastyCatFood


People also ask

How do you remove null from a List in DART?

removeWhere() function We can use contains() function in For Loop to remove empty, null, false, 0 values from a List.

How do you get rid of null in flutter?

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

How do you return a NULL in darts?

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.


1 Answers

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(); 
like image 188
Wesley Chang Avatar answered Nov 10 '22 10:11

Wesley Chang