以https://kotlinlang.org/docs/reference/coroutines/flow.html#flows-are-cold为例
fun simple(): Flow<Int> = flow {
println("Flow started")
for (i in 1..3) {
delay(100)
emit(i)
}
}
fun main() = runBlocking<Unit> {
println("Calling simple function...")
val flow = simple()
println("Calling collect...")
flow.collect { value -> println(value) }
println("Calling collect again...")
flow.collect { value -> println(value) }
}我在collect上发现了错误。
This is an internal kotlinx.coroutines API that should not be used from outside of kotlinx.coroutines. No compatibility guarantees are provided.It is recommended to report your use-case of internal API to kotlinx.coroutines issue tracker, so stable API could be provided instead当我添加@InternalCoroutinesApi时
@InternalCoroutinesApi
fun main() = runBlocking<Unit> {
println("Calling simple function...")
val flow = simple()
println("Calling collect...")
flow.collect { value -> println(value) }
println("Calling collect again...")
flow.collect { value -> println(value) }
}我在collect的lambda (函数value -> println(value)中得到了一个错误,如下所示
Type mismatch.
Required:
FlowCollector<Int>
Found:
([ERROR : ]) → Unit
Cannot infer a type for this parameter. Please specify it explicitly.我使用的是Kotlin版本1.4.21。
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.2"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2'我做错了什么不能在Android中编译示例代码的错误吗?
发布于 2021-01-04 09:07:14
答案是,不,collect不仅仅是内部的kotlinx.coroutines API。错误信息具有误导性。
根据@ir42的评论,add import kotlinx.coroutines.flow.collect解决了这个问题。
附加信息,为什么我不选择作为答案
collect和collectLatest是不同的。
使用此示例
fun simple(): Flow<Int> = flow { // flow builder
for (i in 1..3) {
delay(100) // pretend we are doing something useful here
emit(i) // emit next value
}
}
fun main() = runBlocking<Unit> {
// Launch a concurrent coroutine to check if the main thread is blocked
launch {
for (k in 1..3) {
println("I'm not blocked $k")
delay(100)
}
}
// Collect the flow
simple().collect { value -> println(value) }
}收集将产生
I'm not blocked 1
1
I'm not blocked 2
2
I'm not blocked 3
3根据https://kotlinlang.org/docs/reference/coroutines/flow.html
但collectLatest
fun simple(): Flow<Int> = flow { // flow builder
for (i in 1..3) {
delay(100) // pretend we are doing something useful here
emit(i) // emit next value
}
}
fun main() = runBlocking<Unit> {
// Launch a concurrent coroutine to check if the main thread is blocked
launch {
for (k in 1..3) {
println("I'm not blocked $k")
delay(100)
}
}
// Collect the flow
simple().collectLatest { value -> println(value) }
}将产生
I'm not blocked 1
I'm not blocked 2
1
I'm not blocked 3
2发布于 2021-11-18 20:37:20
增加以下进口:
import kotlinx.coroutines.flow.collect发布于 2022-06-23 12:49:10
在它差点把我逼疯后,我终于找到了解决办法。由于这里没有一个解决方案对我有用,所以我有一个特例,我正在处理一个多模块项目,而collect()一直在到处抛出这个错误,甚至手动导入它也没有修复它。
对我来说,解决这个问题的方法是向我的项目的所有模块build.gradle文件中显式地添加协同依赖项,而不仅仅是应用程序的一个,甚至那些不使用coroutines的模块。
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2")https://stackoverflow.com/questions/65559153
复制相似问题