Skip to content

Commit c935525

Browse files
authored
Merge pull request #125 from hpoul/wherenotnull-cast
whereNotNull should transform generic type to non-nullable.
2 parents a446b94 + 401f5b0 commit c935525

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/src/iterable.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,9 @@ extension IterableFilterNotToIndexed<E> on Iterable<E> {
623623
whereNotToIndexed(destination, predicate);
624624
}
625625

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

631631
extension IterableWhereIndexed<E> on Iterable<E> {
@@ -717,9 +717,9 @@ extension IterableWhereNotToIndexed<E> on Iterable<E> {
717717
}
718718
}
719719

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

725725
extension IterableMapNotNull<E> on Iterable<E> {

test/iterable_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ void main() {
475475
expect([0, null, 1, null, null, 2].whereNotNull(), [0, 1, 2]);
476476
});
477477

478+
test('.whereNotNull()', () {
479+
expect([0, null, 1, null, 2].whereNotNull().map((e) => e + 1), [1, 2, 3]);
480+
});
481+
478482
test('.mapNotNull()', () {
479483
expect([].mapNotNull((it) => 1), []);
480484
expect([1, 2, 3, 4].mapNotNull((it) => null), []);

0 commit comments

Comments
 (0)