Jetpack Compose Show Snackbar

Assuming I try to load data from firestore, and I would show error via snackbar.

@Composablefun JourneyApp(viewModel: MainViewModel = viewModel()) {    // snackbar setup    // allow sequential snackbar message without overwriting each other    val snackbarChannel = remember { Channel<String>(Channel.BUFFERED) }     val scaffoldState = rememberScaffoldState()    snackbarChannel.trySend("Loading Data ....")    val postsResource by viewModel.fetchPosts().collectAsState(initial = Resource.loading(null))    val posts = postsResource.data ?: emptyList()    if (postsResource.status == Status.ERROR) {        snackbarChannel.trySend(postsResource.message ?: "Error")    }    LaunchedEffect(snackbarChannel) {        snackbarChannel.receiveAsFlow().collect { message ->            scaffoldState.snackbarHostState.showSnackbar(                message = message            )            /*            when (result) {                SnackbarResult.ActionPerformed -> {                    /* action has been performed */                }                SnackbarResult.Dismissed -> {                    /* dismissed, no action needed */                }            }             */        }    }    Scaffold(        scaffoldState = scaffoldState,    ) { innerPadding ->        LazyColumn(modifier = Modifier.padding(innerPadding)) {            items(posts) { post ->                Text(post.title)            }        }    }}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.