Home > Software engineering >  Cannot use list in lazycolumn in kotlin
Cannot use list in lazycolumn in kotlin

Time:07-22

I am using LazyColumn in project. When I am passing the list it giving me error. Can someone guide me what is the error?

enter image description here

UPDATE

enter image description here

CodePudding user response:

You were seeing that error because your nearestResultList is nullable and among the various signatures/overloads of the items(...) function, the signature items(size: Int, ...) was chosen as the "closest match".

The only thing that you need to do, to be able to use any of the items(...) signatures is a null check

import androidx.compose.foundation.lazy.items // or auto-fix imports

if (nearestResultList != null) {
    LazyColumn {
        items(nearestResultList) {
            Text(text = it.event, color = Color.White)
        }        
    }
}

CodePudding user response:

@Composable
fun ResultScreen(nearestResultList: List<NearestResult>?) {
    Column(
        Modifier
            .fillMaxSize()
            .background(getBackgroundColor())
    ) {
        LazyColumn {
            nearestResultList?.size?.let {
                items(it) { index ->
                    Text(text = nearestResultList[index].event, color = Color.White)
                }
            }
        }
    }
}
  • Related