Eaedev
Eaedev

Reputation: 319

Kotlin list.map with if changing returning value

I am on a android app and I am triying to modify this code in order to only add to the List of DataItem if a condition is met (DataItem.PlanificacionItem(it).planificacion.diaP == 0). If I put an if inside the map, it changes from List<DataItem> to List<Any> and if I put an else {null} then i get the following error: kotlin.NoWhenBranchMatchedException

fun addHeaderAndSubmitList(list: List<PlaniFicacion>?) {
        adapterScope.launch {
            val items = when (list) {
                null -> listOf(DataItem.Header)
                else -> listOf(DataItem.Header) + list.map {if (DataItem.PlanificacionItem(it).planificacion.diaP == 0) {
                    DataItem.PlanificacionItem(it) } else {null}} + listOf(DataItem.Header_2)}




            withContext(Dispatchers.Main) {
                submitList(items)
            }
        }
    }

Thanks in advance

Upvotes: 1

Views: 823

Answers (1)

Uriel Frankel
Uriel Frankel

Reputation: 14622

You need to use list.filterNot(it -> it.planificacion.diaP == 0) and not list.map:

Upvotes: 1

Related Questions