[英]Combine Flow<List> and string in kotlin
Hey I am working in kotlin flow.嘿,我正在使用 kotlin flow。 I have flow in which I have list of data coming from server.
我有来自服务器的数据列表的流程。 And I want to filter text, I tried some piece of code but it's giving me problem.
我想过滤文本,我尝试了一些代码,但它给了我问题。 Can someone guide me on this.
有人可以指导我吗? Thanks
谢谢
ExploreViewModel.kt探索视图模型.kt
class ExploreViewModel : BaseViewModel() {
private val query = MutableStateFlow("")
var queryText: String
get() = query.value
set(value) {
query.value = value
}
val filteredTopics = MutableStateFlow<List<ConsultationTopics>>(emptyList())
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.combine(filteredTopics) { queryText, categoriesList ->
val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
return@combine filteredTopics
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
}
}
I have filteredTopics
property which have all data coming from server, queryText
is using onQueryTextChange
from serarchview
.我有
filteredTopics
属性,其中所有数据都来自服务器, queryText
正在使用来自onQueryTextChange
的serarchview
。 I am trying to filter the data and create new property called filteredCategories
to pass this value to adapter.我正在尝试过滤数据并创建名为
filteredCategories
的新属性以将此值传递给适配器。 I am trying in filteredCategories
I am checking queryText
is empty then passing whole list otherwise pass filter list only, but I don't know this is correct way of doing or not.我正在
filteredCategories
中尝试检查queryText
是否为空,然后传递整个列表,否则仅传递过滤器列表,但我不知道这是不是正确的做法。
Error错误
private fun setupFilteredTopic() {
lifecycleScope.launchWhenCreated {
repeatOnLifecycle(Lifecycle.State.CREATED) {
viewModel.filteredCategories.collect { filteredTopicsList ->
consultationAdapter.submitList(filteredTopicsList)
}
}
}
}
Can someone guide me.有人可以指导我。 Thanks
谢谢
The problem is that you returning elements of different types for filteredCategories问题是您为过滤类别返回不同类型的元素
if (criteria.isEmpty()) {
return@combine filteredTopics // type MutableStateFlow<List<ConsultationTopics>>
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true } // type List<ConsultationTopics>
}
so you need to make a little change to your code, change return@combine filteredTopics
by return@combine filteredTopics.value
:所以你需要对你的代码做一点改动,通过 return@combine filteredTopics.value 更改
return@combine filteredTopics
return@combine filteredTopics.value
:
class ExploreViewModel : BaseViewModel() {
private val query = MutableStateFlow("")
var queryText: String
get() = query.value
set(value) {
query.value = value
}
val filteredTopics = MutableStateFlow<List<ConsultationTopics>>(emptyList())
val filteredCategories = query
.debounce(200) // low debounce because we are just filtering local data
.distinctUntilChanged()
.combine(filteredTopics) { queryText, categoriesList ->
val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
categoriesList
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
}
}
this should fix the error这应该修复错误
You should return a list of ConsultationTopics
objects in the combine
block using categoriesList
instead of filteredTopics
:您应该使用
categoriesList
而不是filteredTopics
在combine
块中返回ConsultationTopics
对象列表:
val criteria = queryText.lowercase()
return@combine if (criteria.isEmpty()) {
categoriesList
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
Or we can omit the return@combine
operator:或者我们可以省略
return@combine
运算符:
val criteria = queryText.lowercase()
if (criteria.isEmpty()) {
categoriesList
} else {
categoriesList.filter { category -> category.title?.lowercase()?.let { criteria.contains(it) } == true }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.