How to preview LazyPagingItems/Paging 3 Library in Jetpack Compose

Viewed 1457

I think the title says it all.

LazyPagingItems constructor is internal. I can't pass LazyPagingItems as parameter in Preview Composable, let alone passing sample data. But I want to show preview of my composable, how should I do this?

@Composable
fun MainUi(users: LazyPagingItems<User>) {
    Scaffold {
        LazyColumn() {
            items(users) {
                // Rest of the code..
            }
        }
    }
}

@Preview
@Composable
fun Preview() {
    DefaultTheme {
        MainUi(users = ) // How to pass sample data here?
    }
}
1 Answers

You can use PagingData.empty() or PagingData.from(List<T>), like this:

@Preview
@Composable
fun Preview() {
    DefaultTheme {
        MainUi(users = flowOf(PagingData.from(listOf(User(..)))).collectAsLazyPagingItems()
    }
}

I'm not sure if the items are shown in the preview, but at least you get to render it...

Related