[英]Not receiving updates in StateFlow observable
I have two classes我有两节课
class ChildFragment : ParentFragment {
viewModel.location.collectLatest { result ->
when (result) {
is LocationResult.Unit -> {}
is LocationResult.Error -> {}
is LocationResult.Success -> {} // Not recieving callback here.
}
}
}
class ChildViewModel : ParentViewModel
class ParentFragment
class ParentViewModel {
private val _location = MutableStateFlow<LocationResult>(LocationResult.Unit)
val location = _location.asStateFlow()
fun updateLocation(location: Location) {
_location.value = LocationResult.Success(location)
}
}
I am receiving the first Unit result in the flow callback but does not receive it once I call the update method.我在流回调中收到第一个单元结果,但在调用更新方法后没有收到它。
Anything, in particular, that is being done wrong?有什么特别是做错了吗? Please help.请帮忙。
I would suggest at your fun updateLocation
, implement also with the others like below我会建议在你有趣的updateLocation
中,也可以与下面的其他人一起实施
fun updateLocation(location: Location) {
try {
_location.value = LocationResult.Success(location)
} catch (e: Exception) {
_location.value = LocationResult.Error(e.message)
}
}
with this LocationResult.Error
, you are able to know if there is any error or not.有了这个LocationResult.Error
,您就可以知道是否有任何错误。 Please not to avoid any exception for futures.请不要避免期货的任何例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.