Aaron Waller
06/01/2022, 8:21 AMvar 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 timeoianmol
06/01/2022, 8:26 AMcollectAsState
ste
06/01/2022, 8:34 AMisDisplayed
variable.Aaron Waller
06/01/2022, 8:35 AMLaunchedEffect(key1 = viewModel.snackBarMessage)
its not getting triggered at all.
I changed my mutableStateOf to a MutableStateFlow and collected it as a state but its still the same behaviour, it just gets called the very first timeste
06/01/2022, 8:38 AMsnackBarMessage
is useless. You can directly call scaffoldState.snackbarHostState::showSnackbar
inside your clickable or whateverAaron Waller
06/01/2022, 8:54 AMscope.launch {
scaffoldState.snackbarHostState.showSnackbar("Added to Favorite", actionLabel = "Ok")
}
and it gets called every time.
Thanks guysCasey Brooks
06/01/2022, 2:01 PMLaunchedEffect
isn’t triggering properly, it’s that the mutableStateOf
is optimizing itself when it sees that the new value is the same as the old value. mutableStateOf()
by default uses structuralEqualityPolicy()
to do that optimization, but you can use a different policy such as referentialEqualityPolicy()
or neverEqualPolicy()
to tweak it so that it triggers when you expect it should. In this case, to have it trigger twice on null
, you’ll need the neverEqualPolicy()
var snackBarMessage: MutableState<String?> = mutableStateOf(null, policy = neverEqualPolicy())
private set
Zach Klippenstein (he/him) [MOD]
06/01/2022, 4:21 PMshowSnackbar
function from your event handler is the correct approach. Sure you can use neverEqualPolicy
to maybe make this work, but there’s a reason that path has extra friction. State, by definition, is idempotent, so most types for working with state (MutableState
, MutableStateFlow
) automatically conflate equivalent values.