Search code snippets, questions, articles...

Remove empty and falsey values from a List in Dart/Flutter

If you have a list created in Dart and the list contains empty, null, or falsey values and you want to remove them from the list then the code examples explained in this post can be helpful.
void main() {
  // define a Dart list
  final myList = [10, '', '', null, 20, 0, 40, 50, false];
  // remove falsy values from the list
  myList.removeWhere((item) => ["", null, false, 0].contains(item));
  // print the list


[10, 20, 40, 50]

In the above code example, we are using the .removeWhere() function of the List and inside the function, we are removing the values that are found in ["", null, false, 0] list. If you have more values that you want to remove you can update this list.

removeWhere() function

The removeWhere() function can be used to remove items from a list based on some conditions.

List names = <String>["Tony", "", "", "Ebigel", "", "Steve", ""];

names.removeWhere( (item) => item.isEmpty );



[Tony, Ebigel, Steve]

# Code Example 2: Remove Empty String values using For Loop

We can use Dart For Loop to remove empty string items from a List. below is an example to do that.

List myList = <String>["Rick", "", "John", "", "", "Carol", "Saisha"];

List result = [];

for (var item in myList) {
  if (!item.isEmpty) {



[Rick, John, Carol, Saisha]

# Code example 3: Remove empty, false null and 0 values from List using for loop

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

// define a Dart list
List cars = ["Tesla", "", "Volvo", "", null, 0, false, "Nexon"];

List result = [];

for (var car in cars) {
  if (!["", null, false, 0].contains(car)) {

// print the result list


[Tesla, Volvo, Nexon]
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet