8. Flows
Example 0: Pure flow example
class NewsRemoteDataSource(
private val newsApi: NewsApi,
private val refreshIntervalMs: Long = 5000
) {
val latestNews: Flow<List<ArticleHeadline>> = flow {
while(true) {
val latestNews = newsApi.fetchLatestNews()
emit(latestNews) // Emits the result of the request to the flow
delay(refreshIntervalMs) // Suspends the coroutine for some time
}
}
}val latestNewsState: State<List<String>> =
newsRemoteDataSource.latestNews.collectAsState(initial = listOf())
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
LazyColumn {
items(latestNewsState.value) {
Text(text = it)
}
}
}Example 1: Account creation validation
Last updated
Was this helpful?