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();
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! 馃憤
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???
@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());
)
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.
@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 馃憤
Most helpful comment
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 toinitialStatebecause 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! 馃憤