https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

ms

05/14/2021, 10:07 AM
App crashes when combining Flow with a MutableStateFlow. Code in 🧵
Copy code
val appsFlow: Flow<List<App>>
    get() = appDrawerRepo.allAppsFlow.combine(hiddenAppsRepo.onlyHiddenAppsFlow) { allApps, hiddenApps ->
        allApps.minus(hiddenApps)
    }.combine(searchFlow) { filteredApps, query ->
        when {
            query.isNotEmpty() -> filteredApps.filter {
                it.name.startsWith(
                    query,
                    ignoreCase = true
                )
            }
            else -> filteredApps
        }
    }
Copy code
private val searchAppFlow = MutableStateFlow("")
It works if I remove the second combine or use
listOf("").asFlow()
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object kotlinx.coroutines.flow.Flow.collect(kotlinx.coroutines.flow.FlowCollector, kotlin.coroutines.Continuation)' on a null object reference at kotlinx.coroutines.flow.internal.CombineKt$combineInternal$2$1.invokeSuspend(Combine.kt:145) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69) at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:244) at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:161) at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:369) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:403) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:395) at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:300)
n

natario1

05/14/2021, 1:41 PM
Maybe you are declaring the searchFlow after appsFlow in your file.
e

ephemient

05/14/2021, 1:58 PM
or in a subclass
m

ms

05/14/2021, 2:53 PM
They both are in same class one after other
a

Abhishek Dewan

05/14/2021, 3:03 PM
Based on the error appDrawerRepo.allAppsFlow seems to be null. are you able to collect that flow in isolation and not using combine ?
m

ms

05/14/2021, 3:08 PM
Yes, I'm unable to use it with combine. But instead of
searchFlow
if I give
listOf("").asFlow()
to combine it works.
Even if I use the combine in reverse order, it crashes at the first combine call
l

louiscad

05/14/2021, 11:34 PM
Which kotlinx.coroutines version are you using?
e

ephemient

05/14/2021, 11:43 PM
I think an initialization problem is more likely. you can try to insert a
.also { println(searchFlow) }
before the
.combine(searchFlow) { ... }
m

ms

05/15/2021, 2:57 AM
This is happening for every
MutableStateFlow
even though I initialized it. I'm using Jetpack Compose 1.0.0-beta06
l

louiscad

05/15/2021, 9:21 AM
@ms I asked a question.
m

ms

05/15/2021, 9:49 AM
@louiscad I'm using Jetpack Compose, no explicit import
l

louiscad

05/15/2021, 10:03 AM
Are you able to reproduce the issue in a simple JVM program?
m

ms

05/15/2021, 10:16 AM
@louiscad It worked when I tried it in Kotlin Playground. Maybe it's something from Compose side I guess
3 Views