Kotlin: MutableSharedFlow instance is null during emit even for nonNull variable

34 Views Asked by At

I have the following class:

internal class TestClass(
     private val someOtherFlow: Flow<String>
) {
    private val scope: CoroutineScope by lazy {
        CoroutineScope(SupervisorJob())
    }
    private val testFlow: MutableSharedFlow<String> = MutableSharedFlow()
    init {
        scope.launch {
            someOtherFlow.collect {
                refreshTestFlow()
            }
        }
    }
    private fun refreshTestFlow() {
        scope.launch {
            testFlow.emit(strValue)
        }
    }
}

testFlow.emit(strVlue) --> testFlow is giving NullPointerException even though it is a nonNull variable and instantiated when the TestClass is created.

Could someone please help understand why this is happening. Thanks

I have tried testing this.

0

There are 0 best solutions below