Reputation: 11
I'm trying to implement a messaging app on Jetpack Compose using Firebase's Realtime Database. I am able to fetch the data successfully as suggested in the documentation, and I'm able to display the data that is fetched. But when new data is added to the snapshot, the data changes on Firebase but doesn't display on-screen.
Here's how I'm fetching the data in my View Model from the Repository:
val dataStateFlow = MutableStateFlow<MutableList<Message>>(mutableListOf())
init {
viewModelScope.launch {
messagingRepo.getData().collect {
dataStateFlow.value = it
}
}
}
And here's how I'm doing it in the Composable:
val data = messagingVM.dataStateFlow.asStateFlow().collectAsState().value
I'm certain the issue is with transferring the updated data from my Repository to the Composable, since I am not using the single value event listener. Thanks for your help in advance!
Upvotes: 1
Views: 584
Reputation: 1042
Try using by
and remember
like this instead of .value
:
val data by remember { messagingVM.dataStateFlow.asStateFlow() }.collectAsState()
Your data will be updated in compose. If it's not, make sure it's updated in the viewModel.
Upvotes: 1