LaunchedEffect not triggered when mutableStateOf has same value?
I want to display a snackbar message when the user clicks on “Favorite Post”
It works fine the first time but after that it is not getting triggered anymore.
Here is my snackBarMessage State in ViewModel:
var snackBarMessage: MutableState<String?> = mutableStateOf(null)
private set
and the set function:
fun setSnackbarMessage(msg: String?){
snackBarMessage.value = null
snackBarMessage.value = msg
}
Here is my Launcheffect which unfortunately is not getting triggered twice:
LaunchedEffect(key1 = viewModel.snackBarMessage.value){
val message = viewModel.snackBarMessage.value
message?.let {
scaffoldState.snackbarHostState.showSnackbar(message, "Ok")
}
}
and on Favorite button click I call:
viewModel.setSnackbarMessage("Added to Favorite")
Eventhough I set the snackBarMessage to null it is not getting retriggered, only if the string message is different from the one before.
I want to show the snackbar every time the user clicks on the button and not just the first time