Search code examples
androidandroid-jetpack-composeandroid-jetpack

How do I iterate over a Map List in LazyRow?


I'm trying to get the images from the CatModel into my LayzColumn. But I don't know how because I have a map list

          LazyRow(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    contentPadding = PaddingValues(horizontal = 16.dp),
                    modifier = Modifier.fillMaxWidth()
                )
                {
                    items(dish.integrates.entries.toList()) { entry ->
                        AsyncImage(
                            model = entry.value.imageUrl, 
                            contentDescription = null,
                            modifier = Modifier.size(50.dp),
                            contentScale = ContentScale.Crop
                        )
                    }


                }

DishDetailModel.kt:

 data class DishDetailModel(
  var title: String? = null,
  var description: String? = null,
  var image: String? = null,
  var time: String? = null,
  var category: ArrayList<String> = ArrayList(),
  var integrates: Map<String, CastModel>
) : Serializable

CastModel.kt:

 data class CastModel(
  var castName: String? = null,
  var imageUrl: String? = null 
 ) : Serializable

Solution

  • There are multiple items functions. You probably use the wrong one, resulting in this error:

    Type mismatch: inferred type is List<Map.Entry<String, CastModel>> but Int was expected

    The LazyListScope (the environment the LazyRow lambda provides) only has a version that takes an Int (the number of items) as a parameter. There are other items functions, but they are declared elsewhere, as extension functions. To use them they must explicitly be imported.

    When you place the cursor at items and press Alt+Space you see a list of available functions. Choose the one that takes a List and the following import statement should be generated:

    import androidx.compose.foundation.lazy.items
    

    The code should compile fine now.