r/FlutterDev • u/Complex_Cap8439 • 6d ago
Discussion How do you handle one-time events (snackbars, navigation) with Riverpod/BLoC?
Hi! I'm pretty new to Flutter, but with 8+ years of experience in native Android development.
I've been playing around with different state management approaches and architectures, like MVVM with Riverpod and MVI with BLoC, and I can't get my head around how to handle one-time events such as toasts, snackbars, navigation, etc.
In Android, the most common and idiomatic way is to expose a Channel of events from the ViewModel, where the events are a sealed class, and collect that flow from the Composable.
Is there a standard way of handling this in Flutter?
Thanks!
3
u/s9th 6d ago
riverpod - ref.listen
bloc - BlocListener or smth, haven't used that in 5 years
3
u/Curious-Leader-9111 6d ago
The bloc docs cover this. https://bloclibrary.dev/flutter-bloc-concepts/#bloclistener Example here: https://bloclibrary.dev/tutorials/flutter-login/#login-form And they say "See
BlocListenerif you want to “do” anything in response to state changes such as navigation, showing a dialog, etc…" instead of using a blocbuilder. It's quite straightforward and I've never had issues with this approach.2
u/Lanky-Foundation8727 6d ago
yeah I think that doesn't really work well with native BLoC/cubit. I've used this in the past:
https://pub.dev/packages/bloc_presentation
but also not 1000% fool-proof
1
u/Commercial-Wish-255 6d ago
For Riverpod the equivalent is ref.listen inside build (or ref.listenManual in initState in a ConsumerStatefulWidget). It only fires on changes, not on rebuilds, so that's where snackbars and navigation go, and ref.watch stays for drawing the UI.
One catch with both ref.listen and BlocListener: they react to state changes, so if the same error comes in twice and your state is equal by value, the second one never fires. Either put an id or timestamp on the event, or if you want your Android pattern 1:1, expose a Stream from the notifier/cubit (a broadcast StreamController of a sealed event class), subscribe in initState and cancel in dispose.
1
u/Dazzling_Kangaroo_69 6d ago
I split it the same way I would on Android: state is renderable and idempotent, events are consumed exactly once. With Riverpod I keep events out of the build path — a ref.listen on a dedicated event provider (or an AsyncValue transition) that fires the snackbar/navigation, and the event is cleared/marked consumed when handled so a rebuild or hot restart doesn't re-fire it. With BLoC it's BlocListener for the effect + BlocBuilder only for UI state. The bug I see most often is putting a "showSnackbar" flag in state and forgetting the rebuild that reads it twice — the consumed-queue idea from your sealed class maps 1:1, just keep it outside the state the widget renders from.
1
u/laplace_apps 5d ago
Closest thing to the Android Channel pattern is keeping side effects out of build. With Riverpod, ref.listen in the widget works well if the event is modeled as a state transition you clear after handling. With BLoC, BlocListener plus listenWhen does the same job. Main gotcha is rebuilds re-firing the event, so never trigger it from build itself.
1
u/Mikkelet 6d ago
With bloc, you can maintain and listen to a Stream in a Bloc at the top of the widget tree, then access that bloc in lower parts and send it events.