TastyCatFood
TastyCatFood

Reputation: 1772

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

Upvotes: 68

Views: 40784

Answers (8)

Tin Man
Tin Man

Reputation: 888

Starting with Dart 3.0, Dart has a new built-in method for getting non-null elements from any iterable — nonNulls.

This method returns an iterable, so if you want a List type, you'll have to convert it to one using toList().

Here's some sample usage:

final a = [null, 2, null];
final b = a.nonNulls.toList();

If you don't have use for the original list, you can call nonNulls on it directly:

final a = [null, 2, null].nonNulls.toList();

Upvotes: 4

CybeX
CybeX

Reputation: 2416

To summarize and provide an improved solution, one could use extensions on Iterable<E> as expressed by @BansookNam's answer.

extension NotNullIterable<E> on Iterable<E?> {
  Iterable<E> whereNotNullable() => whereType<E>();
}

Sample usage

final nonNullList = [1, 2, 3, null, 4].whereNotNullable().toList();

A better solution for this problem is per @felix-ht's suggestion by making use of package:collection/collection.dart

Sample usage

import 'package:collection/collection.dart';

//...

final nonNullList = [1, 2, 3, null, 4].whereNotNullable().toList();

Upvotes: 0

felix-ht
felix-ht

Reputation: 2085

With null-safety the old removeWhere solution does no longer work, if the type of the resulting list is to be non-nullable. Casting after removeWhere doesn't work as well.

The best option is to import collection

import 'package:collection/collection.dart';

which allows one to do

List<int> a = [null, 2, null].whereNotNull().toList();
print(a);
>>> [2]

Upvotes: 71

Bansook Nam
Bansook Nam

Reputation: 591

I made an extension function with @Wesley Changs answer.

  extension ConvertIterable<T> on Iterable<T?> {
    List<T> toNonNullList() {
      return this.whereType<T>().toList();
    }
  }

You can use like below.

  List<int?> a = [null,2,null];
  final List<int> b = a.toNonNullList();

Upvotes: 4

Nick Ribal
Nick Ribal

Reputation: 2109

In modern Dart you can use "collection if" and "collection for", which are similar to Python's list comprehensions:

List a = [null, 2, null];
List b = [for (var i in a) if (i != null) i];
print(b); // prints [2]

Source: https://github.com/dart-lang/language/blob/master/accepted/2.3/control-flow-collections/feature-specification.md#composing

Upvotes: 9

Wesley Chang
Wesley Chang

Reputation: 1741

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

Upvotes: 168

Alan Knight
Alan Knight

Reputation: 2971

  a.where((x) => x != null).forEach(print);

Upvotes: 14

stwupton
stwupton

Reputation: 2235

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

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

Upvotes: 57

Related Questions