kotlin Composable在更改MutableState〈Flow〈List>>值时保持无限循环< Object>

pb3skfrl  于 2023-03-24  发布在  Kotlin
关注(0)|答案(1)|浏览(205)

我正在从firebase组成一个懒惰的项目行。我想有一个按钮的地方类型过滤行(eidogg.,商店,餐馆等)工作正常。由于它们都是完全相同的屏幕,我只是用所选的类别为它调用可组合的,然后,我希望能够选择一个新按钮来进一步过滤,但由于某种原因,组合工具陷入了循环。

fun LocationScreen(
    modifier: Modifier = Modifier,
    viewModel: HomeViewModel = hiltViewModel(),
    useCategoryTag: CategoryTags? = null,
    onLocationClick: (String) -> Unit,
    onBackClick: () -> Unit = {}
) {
        val locations = viewModel.getCategoryLocations(useCategoryTag).value.collectAsStateWithLifecycle(emptyList())
        ...

在我的视图模型中

var locations: MutableState<Flow<List<Location>>> =
        mutableStateOf(emptyFlow())

...
...
...

fun getCategoryLocations(tag: CategoryTags?): MutableState<Flow<List<Location>>> {
        if (tag == null) {
            Log.i("getCategoryLocations","null")
            locations.value = firestoreRepository.locations
        } else {
            Log.i("getCategoryLocations","not null")
            locations.value = firestoreRepository.getLocationCategory(tag, locationTags.toList())
        }
        return locations
    }

我的数据访问(过滤只是一个测试值ATM)

override fun getLocationCategory(
        tag: CategoryTags,
        filters: List<LocationTags>
    ): Flow<List<Location>> {
        return firestore.collection(LOCATIONS_COLLECTION)
            .whereEqualTo("category", tag.toString())
            .snapshots()
            .map { snapshot ->
                snapshot.toObjects<Location>()
                    .filter { location -> location.top_tags[0] == "Few People" }
            }
    }

当一个按钮被点击的时候,点击触发就像这样,if null检查看起来工作得很好(它只是导航)

onClick = {
    if (useCategoryTag == null) {
        viewModel.onCategoryClick(item as CategoryTags, onLocationClick)
    } else {
       viewModel.getCategoryLocations(useCategoryTag)
    }
}

Log就是这样做的:

2023-03-21 20:47:24.876 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:24.893 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:24.917 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:24.948 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:24.978 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:24.997 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:25.028 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:25.044 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null
2023-03-21 20:47:25.060 13143-13143 getCategoryLocations    com.app.senseaid                     I  not null

已尝试使用if语句检查是否应重新组合,但没有任何进展
编辑:现在也在循环初始构图

qyyhg6bp

qyyhg6bp1#

这里有几个问题,但本质上您使用的状态和流是错误的。
试试这个:
在ViewModel中:

val locations = MutableStateFlow<List<Location>>>(emptyList())
...
fun getCategoryLocations(tag: CategoryTags?) {
    if (tag == null) {
        Log.i("getCategoryLocations","null")
        locations.update { firestoreRepository.locations }
    } else {
        Log.i("getCategoryLocations","not null")
        locations.update { firestoreRepository.getLocationCategory(tag, locationTags.toList()) }
    }
}

在“可合成”屏幕中:

val locations by viewModel.locations.collectAsStateWithLifecycle()

相关问题