File tree 2 files changed +9
-3
lines changed
kotlinx-coroutines-core/common
2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -124,7 +124,7 @@ public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
124
124
* The terminal operator that returns the first element emitted by the flow and then cancels flow's collection.
125
125
* Returns `null` if the flow was empty.
126
126
*/
127
- public suspend fun <T : Any > Flow<T>.firstOrNull (): T ? {
127
+ public suspend fun <T > Flow<T>.firstOrNull (): T ? {
128
128
var result: T ? = null
129
129
try {
130
130
collect { value ->
@@ -138,10 +138,10 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
138
138
}
139
139
140
140
/* *
141
- * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
141
+ * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
142
142
* Returns `null` if the flow did not contain an element matching the [predicate].
143
143
*/
144
- public suspend fun <T : Any > Flow<T>.firstOrNull (predicate : suspend (T ) -> Boolean ): T ? {
144
+ public suspend fun <T > Flow<T>.firstOrNull (predicate : suspend (T ) -> Boolean ): T ? {
145
145
var result: T ? = null
146
146
try {
147
147
collect { value ->
Original file line number Diff line number Diff line change @@ -127,6 +127,12 @@ class FirstTest : TestBase() {
127
127
assertNull(emptyFlow<Int >().firstOrNull { true })
128
128
}
129
129
130
+ @Test
131
+ fun testFirstOrNullWithNullElement () = runTest {
132
+ assertNull(flowOf<String ?>(null ).firstOrNull())
133
+ assertNull(flowOf<String ?>(null ).firstOrNull { true })
134
+ }
135
+
130
136
@Test
131
137
fun testFirstOrNullWhenErrorCancelsUpstream () = runTest {
132
138
val latch = Channel <Unit >()
You can’t perform that action at this time.
0 commit comments