Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot use list in lazycolumn in kotlin

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

ResultScreen.kt

@Composable
fun ResultScreen(nearestResultList: List<NearestResult>?) {
    LazyColumn(
        Modifier
            .fillMaxSize()
            .background(getBackgroundColor())
    ) {
        items(nearestResultList) { nearestResult ->
            Text(text = "$nearestResult")
        }
    }
}

Error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Type mismatch.
Required:
Int
Found:
List<NearestResult>?

enter image description here

UPDATE

enter image description here

>Solution :

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)
        }        
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading