felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.69k stars 3.38k forks source link

bloc reset to initial state #430

Closed amreniouinnovent closed 5 years ago

amreniouinnovent commented 5 years ago

Is your feature request related to a problem? Please describe. I have multi Bloc Builders I need to switch between state by checking if the current state is in the initial state or not.

Describe the solution you'd like I know that I can just dispatch an event like LogoutEventor ResetEventbut I think every bloc should have a reset function so instead of an event for reset https://github.com/felangel/bloc/blob/ef0860e3aaf552a8aebb09610f16ef611cef9dd0/examples/flutter_login/lib/login/login_bloc.dart#L36 it could be BlocProvider.of<LoginBloc>(context).reset();

felangel commented 5 years ago

Hi @amreniouinnovent ๐Ÿ‘‹ Thanks for opening an issue!

I don't think it's a good idea to introduce a reset() because it directly goes against the bloc library paradigm: the only way to trigger a state change is by dispatching an event. I think for cases like you mentioned it is better to have an explicit event to change the state back to initialState because it will allow you to have a record of what caused the state to be reset. Otherwise, if anyone can call reset from anywhere, it will not be possible to conclude what triggered the reset.

Hope that makes sense! ๐Ÿ‘

camh3190 commented 5 years ago

Hi @felangel , I have a question and I think it is related to this.

I have a Page(Screen), which receives an Id and gets some information from an API every time it is opened. I thought to use the initialState to make the API call, thinking that initialState execute every time I open the page, now I understand this is not.

But, if I dispatch an event to change the state to initialState again when call 'Navigator.pop(context)' to close the page, the API call will execute unnecessarily.

It should exist a better solution than adding a flag(probably a boolean var XD ) to check if the page it is opening or closing

So, I think my question is: how to call an API to get some information, from initialState and do not call the API unnecessarily when resetting the state to closing the page???

amreniouinnovent commented 5 years ago

@camh3190 Do Visibility Detector is helpful for you?

VisibilityDetector(
   key: Key("unique key"),
   onVisibilityChanged: (VisibilityInfo info) {
       debugPrint("${info.visibleFraction} of my widget is visible");
   },
   child: MyWidgetToTrack());
)

https://pub.dev/packages/flutter_widgets

camh3190 commented 5 years ago

HI @amreniouinnovent, thanks for the suggestion. I was trying to use it but in the end, I give up. I'm not sure if I using wrong the keys or all Visibility Detector widget(probably), but the "onVisibilityChanged" triggers every time a formField get the focus (click the field). I did not mention it, the page has a form. Then, the reset event fires and reload the information on the page and makes impossible write.

So, I decides to use "the old reliable" = D and add a boolean var. I got the results I was looking for and for now, I satisfy. In another case, with more knowledge of flutter and bloc, I will try it again.

felangel commented 5 years ago

@camh3190 I'm not sure I fully understand the issue but in general you should build your app so that your widget's build method can be executed 100s of times. When focus changes, for example, Flutter will retrigger the build method. I would recommend not having any side-effects in your build method. If you want to execute a snippet of code in response to a bloc state change, you should use BlocListener instead.

Hope that helps ๐Ÿ‘

SvintXprecious commented 2 years ago

@felangel Hey I'm creating an ecommerce app. However,I have implemented the checkout bloc which pushes the order to firestore but the cart state still remains after checking out and the once the user tries to check out again the previous checkout state is not disposed.Any way to clear the state of a bloc?

felangel commented 2 years ago

@SvintXprecious I highly recommend scoping your bloc lower in the widget tree so that is can be automatically disposed when itโ€™s no longer needed. This way you donโ€™t need to manually add a reset event. Hope that helps!

nicolas-ferrada commented 7 months ago

Helll @felangel, the only issue I have with scoping the blocs lower to avoid a reset event, is that you will need to manage closing the blocs manually when using BlocProvider.value, which seems like ending up in the same problem, but instead of a reset event, needing a 'closing event'.