如何在 Kotlin 中将 Flow<T> 转换为 Flow<List<T>>?

问题描述

我有一个完整的项目流,比如 Flow。我想将此流转换为 Flow,其中包含所有项目的列表。

我试过使用

public fun <T> Flow<T>.toList(): Flow<List<T>> = flow {
    val list = toList(mutablelistof())
    emit(list.toImmutableList())
}

但是这个函数永远不会发出值

解决方法

如果您真正想要的是包含单个 Flow.toList() 元素的流,那么我认为您做对了。 单个元素将是初始流中所有项目的列表。

我认为您的问题可能是 fun main(args: Array<String>) = runBlocking { val initialFlow = List(5) { it }.asFlow() initialFlow.toSingleListItem().collect { println(it) } } public fun <T> Flow<T>.toSingleListItem(): Flow<List<T>> = flow { val list = toList(mutableListOf()) emit(list) } 函数已经存在。 尝试以不同的方式命名:

// Query: Get failed operations for migrated apps
let migrationsTimeDiff = 15d;
let operationsDiffTime = 24h + 1m;
let migratedApps = FirstTable
    | where TimeStamp >= ago(migrationsTimeDiff)
    | where MetricName == "JobSucceeded"
    | project
        MigrationTime = PreciseTimeStamp,appName = tostring(parse_json(Annotations).AppName)
    | project appName;
SecondTable
    | where TimeStamp > ago(operationsDiffTime)
    | where Url has_any (appName)
    | where Result == "Fail" 

你可以在这里运行它: https://pl.kotl.in/frCg831WM