Skip to content
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

feat: ability to observe state directly via context.watch #4330

Open
btrautmann opened this issue Jan 13, 2025 · 2 comments
Open

feat: ability to observe state directly via context.watch #4330

btrautmann opened this issue Jan 13, 2025 · 2 comments
Assignees
Labels
enhancement candidate Candidate for enhancement but additional research is needed pkg:flutter_bloc This issue is related to the flutter_bloc package
Milestone

Comments

@btrautmann
Copy link

Description

Oftentimes I am bitten by the fact that doing the following is not supported:

final state = context.select((MyState s) => s.canContinue);

Instead, I need to do:

final state = context.select((MyCubit c) => c.state.canContinue);

The former is a bit more ergonomic than the latter, and is something you can see done via StateNotifier (as mentioned in the README, sorry...no closer anchors).

Desired Solution

I haven't looked at bloc's code, so this might be dumb, but I would suspect that another Provider<T> could be shimmed in such that BOTH (the cubit and its state) are provided and the consumer can select which they want to observe.

Alternatives Considered

Me just dealing with it 💁‍♂️

Additional Context

You can see the original discussion RE: this issue in Discord.

@felangel felangel added enhancement candidate Candidate for enhancement but additional research is needed pkg:flutter_bloc This issue is related to the flutter_bloc package labels Jan 13, 2025
@felangel felangel self-assigned this Jan 13, 2025
@felangel felangel added this to the v9.1.0 milestone Jan 13, 2025
@felangel
Copy link
Owner

Hey @btrautmann 👋
Thanks for opening an issue!

I agree that the ergonomics of context.select (as well as the other extension methods) could be greatly improved. The reason for the current API is that it just leverages the pkg:provider implementation to provide a unified API. I believe the StateNotifier example you're referencing uses pkg:riverpod but I'll take a closer look.

I think the best option is to introduce new extensions to avoid conflicting with pkg:provider and we'd just need to come up with some new names for context.read, context.select, and context.watch. Will think about it some more and any other ideas are welcome 👍

@saschaernst
Copy link

In order to create extensions to watch/select a state directly, you would have to type both the Bloc/Cubit and the state, which would defy the purpose of making the expression less verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement candidate Candidate for enhancement but additional research is needed pkg:flutter_bloc This issue is related to the flutter_bloc package
Projects
None yet
Development

No branches or pull requests

3 participants