我从数据库中获取页面数据,我有一个返回流的存储库.
class RepositoryImpl (private val db: AppDatabase) : Repository {
override fun fetchData (page: Int) = flow {
emit(db.getData(page))
}
}
在ViewModel中,我调用stateIn(),第一个页面到达,但是如何请求第二个页面呢?通过调用fetchData(第2页),我得到了一个新的流,我需要数据到达旧的流.
class ViewModel(private val repository: Repository) : ViewModel() {
val dataFlow = repository.fetchData(page = 1).stateIn(viewModelScope, WhileSubscribed())
}
如何获取数据流中的第二页?