Bloc: Question - Dispatch an event with parameters that depends on the state

Created on 9 May 2019  ยท  3Comments  ยท  Source: felangel/bloc

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?

question

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:

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();
  }

All 3 comments

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! ๐Ÿ‘

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MahdiPishguy picture MahdiPishguy  ยท  3Comments

ricktotec picture ricktotec  ยท  3Comments

timtraversy picture timtraversy  ยท  3Comments

craiglabenz picture craiglabenz  ยท  3Comments

nhwilly picture nhwilly  ยท  3Comments