Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preview LazyPagingItems/Paging 3 Library in Jetpack Compose

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?
    }
}
like image 395
Risal Fajar Amiyardi Avatar asked Sep 09 '25 18:09

Risal Fajar Amiyardi


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...

like image 150
Tiago Nunes Avatar answered Sep 12 '25 10:09

Tiago Nunes