Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

UPDATE

enter image description here

like image 587
Vivek Modi Avatar asked Dec 06 '25 06:12

Vivek Modi


2 Answers

The correct solution is to use this import:

import androidx.compose.foundation.lazy.items

The problem is that the items function that accepts a list is defined as an Extension function, so we need to import it to make it visible for use.

like image 138
Sarthak Mittal Avatar answered Dec 08 '25 01:12

Sarthak Mittal


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)
        }        
    }
}
like image 21
Ma3x Avatar answered Dec 08 '25 02:12

Ma3x



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!