Kotlinx.coroutines: StateFlow in Multiplatform common code

Created on 3 Jul 2020  路  3Comments  路  Source: Kotlin/kotlinx.coroutines

StateFlow and MutableStateFlow are not available in Kotlin multiplatform common code:

import kotlinx.coroutines.flow.StateFlow
Unresolved reference: StateFlow

Are these only available to platform-specific code? It seems to work fine in e.g. android code

Gradle:

sourceSets {
    def coroutines_version = "1.3.7"
    commonMain {
            dependencies {
                implementation(kotlin('stdlib-common'))
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")
                // ...
            }
        }
    // ...
}
question

Most helpful comment

You should add a dependency on "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version" artifact. Does it help?

Just a note for anyone reading this that is using Kotlin 1.4+ -- this is no longer correct. The commonMain dependency is now simply org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version. See https://kotlinlang.org/docs/reference/migrating-multiplatform-project-to-14.html.

All 3 comments

You should add a dependency on "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version" artifact. Does it help?

Oh... that does help, thank you!

You should add a dependency on "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version" artifact. Does it help?

Just a note for anyone reading this that is using Kotlin 1.4+ -- this is no longer correct. The commonMain dependency is now simply org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version. See https://kotlinlang.org/docs/reference/migrating-multiplatform-project-to-14.html.

Was this page helpful?
0 / 5 - 0 ratings