I’m trying to understand why my SnackBar does not show second time, I’m propagating the snackbar message and error boolean values from my composables, I’m taking this data and emiting a single snackbar but it works first time and not second time with the same message even if I set the message to null.
When I go to my register screen I emit an error message if the e-mail is already present in the DB, then if I trigger again the same message, nothing happens and the snackbar does not appear, any ideas ?
MainActivity
setContent {
var snackBarMessage: String? by remember { mutableStateOf(null) }
var isError by remember { mutableStateOf(false) }
val snackbarHostState = remember { SnackbarHostState() }
CryptoTheme {
Scaffold(
scaffoldState = scaffoldState,
snackbarHost = {
SnackbarHost(hostState = snackbarHostState) { data ->
Snackbar(
snackbarData = data,
backgroundColor = if (!isError) GreenProfit else Color.Red,
contentColor = Color.White
)
}
},
bottomBar = { BottomBarNavigation(navController = navController) },
backgroundColor = Color.Black
) {
NavigationGraph(
modifier = Modifier.padding(bottom = it.calculateBottomPadding()),
navController = navController,
showSnackBarMessage = { message, showError ->
isError = showError
snackBarMessage = message
}
)
LaunchedEffect(key1 = snackBarMessage) {
if (snackBarMessage != null) {
snackbarHostState.showSnackbar(
message = snackBarMessage ?: ""
)
snackBarMessage = null
}
}
}
}
}
Inside NavigationGraph I have multiple composables that reuse this way of propagating the message and error to the scaffold to show the snackbar
NavigationGraph
NavigationGraph(showSnackBarMessage: (message: String, isError: Boolean)) {
Navhost(...) {
composable(route) {
RegisterComposable(showSnackBarMessage)
}
}
and from the composable
RegisterComposable
RegisterComposable(showSnackBarMessage: (message: String, isError: Boolean)){
when(uiState.signUpError) {
showSnackBarMessage("E-mail already in use", true)
}