Vivek Modi
Vivek Modi

Reputation: 7301

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

Upvotes: 17

Views: 4655

Answers (6)

Zobaer Hossain
Zobaer Hossain

Reputation: 23

Updated solution

         LazyColumn {nearestResultList.isNotEmpty() -> {
                items(
                    count = nearestResultList.itemCount,
                    key = nearestResultList.itemKey(),
                    contentType = nearestResultList.itemContentType(
                    )
                ) { index ->
                    val item = nearestResultList[index]
                    if (item == null) {
                       //handle
                    } else {
                        Text(text = it.event, color = Color.White)
                    }
                }
            }}

Upvotes: 0

user1086279
user1086279

Reputation: 111

If you're seeing this issues a LazyHorizontalGrid, make sure you're importing:

import androidx.compose.foundation.lazy.grid.items

If you're seeing this issues a LazyRow, make sure you're importing:

import androidx.compose.foundation.lazy.items

Upvotes: 1

Dmitriy Pavlukhin
Dmitriy Pavlukhin

Reputation: 419

For those, who may use the Paging library, add

import androidx.paging.compose.items

Upvotes: 1

Sarthak Mittal
Sarthak Mittal

Reputation: 6114

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.

Upvotes: 39

Ma3x
Ma3x

Reputation: 6589

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)
        }        
    }
}

Upvotes: 11

Vivek Modi
Vivek Modi

Reputation: 7301

@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)
                }
            }
        }
    }
}

Upvotes: 3

Related Questions