我现在在 JetpackCompose 中有一个问题。问题是,当我从流中收集数据时,值是从 firebase 获取的,就像有一个侦听器一样,并且数据每次都在变化。但事实并非如此。
我不知道真正的问题是什么!
FirebaseSrcNav
suspend fun getName(uid: String): Flow<Resource.Success<Any?>> = flow {
val query = userCollection.document(uid)
val snapshot = query.get().await().get("username")
emit(Resource.success(snapshot))
}
导航库
suspend fun getName(uid: String) = firebase.getName(uid)
主页查看模型
fun getName(uid: String): MutableStateFlow<Any?> {
val name = MutableStateFlow<Any?>(null)
viewModelScope.launch {
navRepository.getName(uid).collect { nameState ->
when (nameState) {
is Resource.Success -> {
name.value = nameState.data
//_posts.value = state.data
loading.value = false
}
is Resource.Failure<*> -> {
Log.e(nameState.throwable, nameState.throwable)
}
}
}
}
return name
}
我认为问题出在 HomeScreen 中,当我调用 collectasState().value 时。
主屏幕
val state = rememberLazyListState()
LazyColumn(
state = state,
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
items(post) { post ->
//val difference = homeViewModel.getDateTime(homeViewModel.getTimestamp())
val date = homeViewModel.getDateTime(post.timeStamp!!)
val name = homeViewModel.getName(post.postAuthor_id.toString()).collectAsState().value
QuestionCard(
name = name.toString(),
date = date!!,
image = "",
text = post.postText!!,
like = 0,
response = 0,
topic = post.topic!!
)
}
}
我无法发布视频,但如果您需要图像,请想象一个文本字段,其中测试每 0.005 秒在“null”和“MyName”之间交替。