feat: Optionally support current state in BlocListener
#4347
Labels
enhancement candidate
Candidate for enhancement but additional research is needed
pkg:flutter_bloc
This issue is related to the flutter_bloc package
Description
Given
BlocListener
is intended to react to changes in state, the current default makes sense: the listener is not called immediately with the current state (as documented).However, I've come across a few instances where it would be useful to have the option for a listener run once with the current state in addition to future transitions. Generally, instances where you want some feedback in the UI based on state which isn't applicable in a
builder
or usingcontext.select
- like snackbars or route changes.Alternatives Considered
The current workaround for this is to make sure the widget is stateful and include an
initState()
. For example, let's say we have a page that is only visible to logged-in users. We want it to re-route users that are not currently logged in in addition to users who log out in future.Alternatively, we use something like
StreamBuilder
with a parameter likeBut this feels way hackier.
Desired Solution
It would clean up a few things to have some sort of optional flag (like
startWithCurrentState
but probably a better name) that will immediately call the listener once with the current state. With this, we could simplify our widget down to be stateless:If it makes more sense this could be its own widget rather than a flag on
BlocListener
.The text was updated successfully, but these errors were encountered: