Taking the direct example from 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) }
}

I get an error in the collect's lambda (function of value -> println(value) as below

Type mismatch.
Required:
FlowCollector<Int>
Found:
([ERROR :  ]) → Unit
Cannot infer a type for this parameter. Please specify it explicitly.

I am using Kotlin version 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 Studio中编译示例代码?

推荐答案

答案是否定的,collect不仅仅是内部kotlinx.coroutines API.错误消息具有误导性.

根据@ir42的 comments ,add import kotlinx.coroutines.flow.collect 个解决了这个问题.

Additional info, why I didn't pick 100 as the answer

collectcollectLatest是不同的.

Using this example

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

But 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

Kotlin相关问答推荐

Kotlin 函数名后面的大括号是什么意思?

kotlin短路列表判断吗?

Kotlin:我可以将函数分配给 main 的伴随对象中的变量吗?

Kotlin 函数中接收者和参数的类型相同

Kotlin 使用委托进行隐式覆盖

使用纯 Kotlin 函数作为 Junit5 方法源

找不到引用的类 kotlin.internal.annotations.AvoidUninitializedObjectCopyingCheck

在 SplashActivity 中显示的 Firebase 应用内消息.如何在 MainActivity 中显示它?

Kotlin 代码是如何编译成原生代码的?

interface扩展

如何退出 Kotlinc 命令行编译器

零安全的好处

如何从 Firestore 查询中排除元素?

Kotlin - 覆盖方法中的 IllegalArgumentException

TextField maxLength - Android Jetpack Compose

为什么 Kotlin Pair 中的条目不可变?

Kotlin:sealed class cannot "contain" data classes?

TornadoFX 中设置 PrimaryStage 或 Scene 属性的方法

Kotlin var lazy init

Kotlin:访问 when 语句的参数