Search code examples
androidandroid-jetpack-composeandroid-pagingandroid-paging-3android-jetpack-compose-lazy-column

LazyColumn error: LazyPagingItems<TypeVariable(T)> required but found List<T>


I'm using the Paging 3 library with LazyColumn and I want to sort the list according to the category of shopping list items. In the code below, LazyColumn complains that it's expecting LazyPagingItems<TypeVariable(T)> for the items property but found List<ShoppingListItem?>. How can I fix this?

Composable

val lazyListState = rememberLazyListState()
val successItems = allItemsState.allItems?.collectAsLazyPagingItems()

LazyColumn(
    state = lazyListState,
    modifier = Modifier
        .fillMaxWidth(),
    contentPadding = PaddingValues(
        start = 5.dp,
        end = 5.dp,
        top = 8.dp,
        bottom = 165.dp
    ),
    verticalArrangement = Arrangement.spacedBy(5.dp),
) {
    val groupedByCategory = successItems!!.itemSnapshotList.groupBy { it!!.category }

    groupedByCategory.forEach { (initial, shoppingListItems) ->
        item {
            Text(text = initial)
        }
        items(
            items = shoppingListItems, //Throws error at this line
            key = { item ->
                item.id
            }
        ) { item ->
            ShoppingListScreenItem(
                item = item,
                mainViewModel = shoppingListScreenViewModel,
                onNavigateToAddEditItemScreenFromItemStrip = { shoppingListItem ->
                    onNavigateToAddEditItemScreenFromItemStrip(shoppingListItem)
                },
            ) { isChecked ->
                scope.launch {
                    shoppingListScreenViewModel.changeItemChecked(
                        item,
                        isChecked
                    )
                }
            }
            Divider(color = Color.LightGray, thickness = 1.dp)
        }
    }
}

Error message

Type mismatch.

Required:
LazyPagingItems<TypeVariable(T)>
Found:
List<ShoppingListItem?>

Solution

  • The appropriate import statement is required in this case.

    If the LazyColumn uses a List<T> for the items DSL method, then the following import statement is required:

    import androidx.compose.foundation.lazy.items
    

    But, if it uses LazyPagingItems<TypeVariable(T)>, then use the following import statement:

    import androidx.paging.compose.items
    

    And both imports can be used at the same time if, for example, you're using a conditional logic to load either of the collection types.