-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[Proposal] Remove Event Stream from Bloc #326
Comments
For me it's usefull feature. And here my use-case: I have TabBar widget, and other child widget can send to MainTabBar dismiss them. But what state I need to map? So, I listen event stream and dismiss TabBar widget at the right moment. |
Hi @felangel, sorry, I missed this thread. Usage: userb.UserBloc.instance.event.listen((event) {
if (event is userb.UserLoggedInBlocEvent) {
String userId = event.loggedInState.user.userId;
app.analytics?.event("login", parameters: {"u": userId});
} else if (event is userb.UserLoggedOutBlocEvent) {
String userId = event.loggedInState.user.userId;
app.analytics?.event("logout", parameters: {"u": userId});
}
}); Emit (simplified): class UserBloc extends Bloc<Event, State> {
@override
Stream<State> mapEventToState(Event event) async* {
if (event is InitializeEvent) {
// Get last state from persistence, try to login the user and if success then
yield _recreateLoggedInState(); // Recreates the state with the proper type (LoggedInWithFacebookUserState/LoggedInWithGoogleUserState)
dispatch(UserLoggedInBlocEvent(loggedInState: currentState, silent: true));
}
else if (event is LoginWithFacebookEvent) {
yield UserLoggingInState(loginEvent: event);
// Login the user and then
yield LoggedInWithFacebookUserState(
user: _blocData.user,
accessToken: _blocData.facebookAccessToken);
dispatch(UserLoggedInBlocEvent(loggedInState: currentState));
}
else if (event is LoginWithGoogleEvent) { /* similar to facebook */ }
else if (event is UpdateUserProfileEvent) {
// Do stuff, like display name change or profile picture upload
yield _recreateLoggedInState();
// No logged in / logged out events
}
else if (event is LogoutEvent) {
State prevState = currentState;
if (prevState is LoggedInUserState) {
await _logoutUser();
yield GuestUserState();
// if UserLoggedOutBlocEvent is not an option, then hard to tell which user logged out
dispatch(UserLoggedOutBlocEvent(loggedInState: prevState));
}
}
}
} That's my particular use case, and despite that I could imagine workarounds like a Or think about a bloc which manage a collection:
Alternatively you can place a I hope these examples are convincing enough or you have better idea for these use cases. |
@janosroden userb.UserBloc.instance.event.listen((event) {
if (event is userb.UserLoggedInBlocEvent) {
String userId = event.loggedInState.user.userId;
app.analytics?.event("login", parameters: {"u": userId});
} else if (event is userb.UserLoggedOutBlocEvent) {
String userId = event.loggedInState.user.userId;
app.analytics?.event("logout", parameters: {"u": userId});
}
}); Anyway you can use the onEvent method in the user bloc for your analytics purpose, and also you can use bloc delegate for the same Why you no use that? Thanks 🤗 |
@xamelon I think that you can have a tabBloc with a provider, and then you can change the state of the tabBar in any part of the widget tree, Thanks. 🤗 |
@basketball-ico
Thanks 🤗 |
@xamelon |
@basketball-ico I don't think moving all these tasks to Despite all of that I understand your consern regarding the direction of the event flow. What do you think about a second stream in a mixin as described in the feature request conversation #259? |
@basketball-ico
|
@janosroden The main problem with you approach is that you are dispatching events, an using this like and output of the bloc. I see and the problem with you You can create an enum AuthState{ notInitialized, loggedIn, loggedOut }`
class AuthBloc extends Bloc<AuthState, AuthState>{
@override
AuthState get initialState => notInitialized;
AuthState mapEventToState(state) async*{
yield state;
}
} Then in your What do you think? |
@xemelon this.mainTabBarBloc.event.listen((MainTabBarEvent event) {
if (event is MainTabBarDismiss) {
Navigator.popUntil(context, ModalRoute.withName('/'));
}
}); In your But if you do this when the user is Logged Out, you can use a |
@basketball-ico
Oh, sorry. When I wrote this, completely forgot about Navigator.popUntil().
Feeling very stupid
Thanks :)
вт, 4 июн. 2019 г. в 13:20, basketball-ico <notifications@github.com>:
… @xemelon
If you only do this when you dispatch themainTabBarDismiss event
this.mainTabBarBloc.event.listen((MainTabBarEvent event) {
if (event is MainTabBarDismiss) {
Navigator.popUntil(context, ModalRoute.withName('/'));
}
});
In your SettingsScreen, you can do directly Navigator.popUntil(context,
ModalRoute.withName('/'));
because this is not business logic, is only ui.
But if you do this when the user is Logged Out, you can use a
blocListester that use the authBloc to do this.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#326?email_source=notifications&email_token=AAS52VJTJVMQ437Q5HHN5RTPYYXT5A5CNFSM4HR6XRU2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW36W6A#issuecomment-498592632>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAS52VI5RDD3SBQ6YRJHDP3PYYXT5ANCNFSM4HR6XRUQ>
.
|
@basketball-ico And you didn't respond to the collection example. If you have a Generally I think state changes are not enough in all cases, sometimes the how and why are matter. As I writing this I'm started wondering maybe somehow put the how and why into the state could be better than the extra events... not sure. |
@janosroden I think the how/why can either be captured in the state itself or the bloc can trigger the actions that you'd like to happen in response to receiving an event. For example, in the If you don't have any strong opposition, I am proposing to deprecate it in a patch and then fully remove it in the next minor version. Let me know and thanks for the discussion! |
Deprecated in bloc v0.14.2 and will be removed in bloc v0.15.0. I'm more than happy to help people refactor their code to not depend on the event stream so don't hesitate to reach out 👍 Thanks everyone! |
What are we supposed to use now instead of the event stream? This was very useful. |
@geopete84 can you please describe your use case? |
Is your feature request related to a problem? Please describe.
Currently, it's possible to do something like:
Describe the solution you'd like
Remove
event
stream since in theory it should not be necessary.Additional context
@basketball-ico rightfully questioned what the use-case was for the feature so
@janosroden since you requested the feature in #259 can you please give your input? It would be awesome to get more context/a concrete use-case for why it is necessary. Thanks 👍
The text was updated successfully, but these errors were encountered: