App crashes when combining Flow with a MutableStat...
# coroutines
m
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
Maybe you are declaring the searchFlow after appsFlow in your file.
e
or in a subclass
m
They both are in same class one after other
a
Based on the error appDrawerRepo.allAppsFlow seems to be null. are you able to collect that flow in isolation and not using combine ?
m
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
Which kotlinx.coroutines version are you using?
e
I think an initialization problem is more likely. you can try to insert a
.also { println(searchFlow) }
before the
.combine(searchFlow) { ... }
m
This is happening for every
MutableStateFlow
even though I initialized it. I'm using Jetpack Compose 1.0.0-beta06
l
@ms I asked a question.
m
@louiscad I'm using Jetpack Compose, no explicit import
l
Are you able to reproduce the issue in a simple JVM program?
m
@louiscad It worked when I tried it in Kotlin Playground. Maybe it's something from Compose side I guess