8000 feat(flutter_bloc): support listener current state by zbarbuto · Pull Request #4405 · felangel/bloc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(flutter_bloc): support listener current state #4405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zbarbuto
Copy link

Status

READY

Breaking Changes

NO

Description

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:

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 #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

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

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
@nvshah
Copy link
Contributor
nvshah commented Apr 18, 2025

@zbarbuto

With this, I guess it will help us avoid writing boiler-plate ie checks in initialState (just to check on last emitted state !!)
Am I interpreting this change rightly ?

Then this indeed would be helpful +1 !!

@zbarbuto
Copy link
Author

@nvshah Yep that's pretty much the exact goal.

@felangel really sorry to ping you but do you have any feedback on this?

@felangel
Copy link
Owner
felangel commented May 2, 2025

@nvshah Yep that's pretty much the exact goal.

@felangel really sorry to ping you but do you have any feedback on this?

Sorry for the delay! Will give this some more thought this weekend.

@tenhobi
Copy link
Collaborator
tenhobi commented Jun 9, 2025

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 onAttach callback described here #4495 would be handy.

I think I like this flag a bit more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Optionally support current state in BlocListener
4 participants
0