Asynchronous 当某些情况发生时,如何停止Kotlin流

Asynchronous 当某些情况发生时,如何停止Kotlin流,asynchronous,kotlin,rx-java,reactive-programming,kotlin-flow,Asynchronous,Kotlin,Rx Java,Reactive Programming,Kotlin Flow,如果代码中出现某些情况,我想取消kotlin流 假设我有一个如下的方法 fun test(): Flow<String> = flow { val lst = listOf("A", "B", "C") while(true) { lst.forEach { emit(it) } //If some condition occurs, need to return from here, else continue //How to

如果代码中出现某些情况,我想取消kotlin流

假设我有一个如下的方法

fun test(): Flow<String> = flow {
    val lst = listOf("A", "B", "C")

    while(true) {
        lst.forEach { emit(it) }

    //If some condition occurs, need to return from here, else continue
    //How to stop flow here
    }
}
问题是,如何在特定条件下(从flow builder或其外部)停止flow来生产任何东西

fun test():Flow=Flow{
val lst=列表(A、B、C)
while(true){
lst.forEach{emit(it)}
如果(某些条件){
return@flow
}
}
}

return@flow
立即从
flow
lambda返回,因此流将结束。作为另一种选择,当您的情况发生时,您可以只
中断
您的
while(true)
循环。

有效。非常感谢。
test().collect { println(it)}