首页 > 解决方案 > 我如何插入列表到列表在科特林

问题描述

我正在尝试将 List<Item?> 类型的列表放入 kotlin 中的另一个 List 类型列表中。

suspend fun insertNotifLocal(list: List<NotificationItems?>) = viewModelScope.async(IO){
    repository?.insertNotifLocal(list)
} 

标签: kotlin

解决方案


我不完全理解您在查看代码片段时要做什么,但根据标题,我可以提供一些输入:

如果您有 aList<Any?>并希望将其作为参数传递:List<Any>您必须确保列表不再包含 null ,例如filterNotNull

fun process(any: List<Any>): Int = any.size

fun main() {
    val nullableList: List<Any?> = listOf(null, 2, "two")
    process(nullableList.filterNotNull())
}

推荐阅读