Hi I have some issues when trying to dispatch an event inside my widget initState ex.
I have 2 different blocs 1 that I'm injecting and trying to get the state from and the other one that belongs to that Screen let's say and I'm dispatching the event.
This is the piece of code that I'm trying to make it work:
@override
void initState() {
final state = (_selectCompanyBloc.state as UserAccountPermissionSelected);
_internalTransfersBloc = InternalTransfersBloc(
orderRepository: OrderRepository(
httpService: HttpService(), secureStorage: SecureStorage()));
_internalTransfersBloc
.dispatch(InternalTransfersFetch(accountPermissionId: state.accountId));
super.initState();
}
I want to use the state inside my initState but I can't cast it to the specific state that has the property because it throws an error.
So my questions is how is the right way to do this using bloc?
Hi @frankrod ๐
Thanks for opening an issue!
Regarding your question, can you please share the error you're getting? I'm guessing that the state is not a UserAccountPermissionSelectedState
at that point. In addition, you also update the InternalTransfersBloc
to have a dependency on SelectCompanyBloc
and within InternalTransfersBloc
you can subscribe to the state stream and dispatch events in response to specific state changes.
If you haven't already, I recommend taking a look at bloc to bloc communication.
Hope that helps! ๐
Hi @felangel
Thanks for the quick response! and for the bloc library pretty good job!
This is the error I'm getting:
flutter: โโโก EXCEPTION CAUGHT BY WIDGETS LIBRARY โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
flutter: The following _CastError was thrown building Builder:
flutter: type 'BehaviorSubject' is not a subtype of type
flutter: 'UserAccountPermissionSelectedState' in type cast
That is the cast error and I'm guessing the error is because 'UserAccountPermissionSelectedState' is a subtype of 'SelectCompanyState' and not the other way around.
Didn't know about that bloc to bloc communication (I'm new to flutter, sorry and didn't read all the documentation)
What I did and seems to work is very similar and you do in a bloc to bloc communication but within the initState again. I came from a react background probably that's why I wanna use it in the initState and didn't know about the bloc t bloc communication so probably it is better to move that listener to the bloc?
@override
void initState() {
_internalTransfersBloc = InternalTransfersBloc(
orderRepository: OrderRepository(
httpService: HttpService(), secureStorage: SecureStorage()));
_someBlocSubscription = _selectCompanyBloc.state.listen((currentState) {
if (currentState is SelectCompanyUserAccountPermissionSelected) {
_internalTransfersBloc.dispatch(InternalTransfersFetch(
accountPermissionId: currentState.userAccountPermission.id));
}
});
super.initState();
}
@frankrod yup glad you got it working!
You might want to use a BlocListener
and dispatch from the listener
because it will handle the subscription for you. I think all of these approaches are very similar but I would recommend either moving it to the bloc entirely to keep the UI clean or if you prefer to listen and dispatch from the UI layer, then try BlocListener
.
Hope that helps! ๐
Most helpful comment
Hi @felangel
Thanks for the quick response! and for the bloc library pretty good job!
This is the error I'm getting:
That is the cast error and I'm guessing the error is because 'UserAccountPermissionSelectedState' is a subtype of 'SelectCompanyState' and not the other way around.
Didn't know about that bloc to bloc communication (I'm new to flutter, sorry and didn't read all the documentation)
What I did and seems to work is very similar and you do in a bloc to bloc communication but within the initState again. I came from a react background probably that's why I wanna use it in the initState and didn't know about the bloc t bloc communication so probably it is better to move that listener to the bloc?