How To Declare Flow<PagingData<Data>> as Variable

42 views Asked by At

I am trying to fetch API data with peging 3 library. I am following this tutorial. However, the API is called multiple times. I assume it's because of this code in compose:

 val documentList = dashboardViewModel.getBreakingNews().collectAsLazyPagingItems()

Here is the code of view model

fun getBreakingNews(

    ): Flow<PagingData<Data>> = getNews().cachedIn(viewModelScope)



    fun getNews() = Pager(
        config = PagingConfig(
            pageSize = 10,
        ),
        pagingSourceFactory = {
            DocPagingSource(arkeliyaApiService)
        }
    ).flow

I want to declare Flow<PagingData<Data>> like I did with other APIs

For example

 private val _listofReqDoc = MutableStateFlow<List<Data>>(listOf())
    val listofReqDoc = _listofReqDoc.asStateFlow()
1

There are 1 answers

0
Jan Bína On

As simple as

val breakingNews = Pager(
    config = PagingConfig(
        pageSize = 10,
    ),
    pagingSourceFactory = {
        DocPagingSource(arkeliyaApiService)
    }
).flow.cachedIn(viewModelScope)

val documentList = dashboardViewModel.breakingNews.collectAsLazyPagingItems()