feat(flutter_bloc): support listener current state#4405
Open
zbarbuto wants to merge 2 commits intofelangel:masterfrom
Open
feat(flutter_bloc): support listener current state#4405zbarbuto wants to merge 2 commits intofelangel:masterfrom
zbarbuto wants to merge 2 commits intofelangel:masterfrom
Conversation
Allows setting a `startWithCurrentState` flag on `BlocListener` which
changes the behaviour to immediately call `listener` with the current
`state` of the bloc before calling again with subsequent emissions.
This allows the view layer to react to whatever the current state is
rather than only when it changes.
For example:
```dart
return BlocListener<AuthBloc, AuthState>(
// Ensure the listener is checked immediately on creation with
// current state in addition to future transitions.
// If the user is somehow routed to this page when not logged in
// they should be immediately kicked out, rather than only when
// they transition from logged in to logged out.
startWithCurrentState: true,
listener: (context, state) {
if(!state.isLoggedIn) {
context.router.replace(const UnauthenticatedRoute());
}
},
child: MyAuthenticatedPageContent(),
);
```
Closes felangel#4347
Contributor
|
With this, I guess it will help us avoid writing boiler-plate ie checks in initialState (just to check on last emitted state !!) Then this indeed would be helpful +1 !! |
Author
Owner
Collaborator
|
I like this idea! In few "rare" cases I also needed or had issues with this, since UI was constructed in some cases later than bloc emited the state and there wasnt better way to handle this to make it stable. So I think either this flag or alternatively having I think I like this flag a bit more. |
abff5bd to
4700d6f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status
READY
Breaking Changes
NO
Description
Allows setting a
startWithCurrentStateflag onBlocListenerwhich changes the behaviour to immediately calllistenerwith the currentstateof the bloc before calling again with subsequent emissions.This allows the view layer to react to whatever the current state is rather than only when it changes.
For example:
Closes #4347
I don't love the name of the flag itself but I couldn't come up with anything better (naming things, amirite?).
Type of Change