Skip to content

Commit

Permalink
Merge pull request #125 from hpoul/wherenotnull-cast
Browse files Browse the repository at this point in the history
whereNotNull should transform generic type to non-nullable.
  • Loading branch information
passsy authored Mar 21, 2021
2 parents a446b94 + 401f5b0 commit c935525
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ extension IterableFilterNotToIndexed<E> on Iterable<E> {
whereNotToIndexed(destination, predicate);
}

extension IterableFilterNotNull<E> on Iterable<E> {
extension IterableFilterNotNull<E> on Iterable<E?> {
/// Returns a new lazy [Iterable] with all elements which are not null.
Iterable<E> filterNotNull() => where((element) => element != null);
Iterable<E> filterNotNull() => whereNotNull();
}

extension IterableWhereIndexed<E> on Iterable<E> {
Expand Down Expand Up @@ -717,9 +717,9 @@ extension IterableWhereNotToIndexed<E> on Iterable<E> {
}
}

extension IterableWhereNotNull<E> on Iterable<E> {
extension IterableWhereNotNull<E> on Iterable<E?> {
/// Returns a new lazy [Iterable] with all elements which are not null.
Iterable<E> whereNotNull() => where((element) => element != null);
Iterable<E> whereNotNull() => where((element) => element != null).cast<E>();
}

extension IterableMapNotNull<E> on Iterable<E> {
Expand Down
4 changes: 4 additions & 0 deletions test/iterable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ void main() {
expect([0, null, 1, null, null, 2].whereNotNull(), [0, 1, 2]);
});

test('.whereNotNull()', () {
expect([0, null, 1, null, 2].whereNotNull().map((e) => e + 1), [1, 2, 3]);
});

test('.mapNotNull()', () {
expect([].mapNotNull((it) => 1), []);
expect([1, 2, 3, 4].mapNotNull((it) => null), []);
Expand Down

0 comments on commit c935525

Please sign in to comment.