Bloc: Function callbacks in Bloc

Created on 26 Mar 2019  路  6Comments  路  Source: felangel/bloc

Currently, the only way of notifying the page of state changes is by yielding a new state. However, there are a lot of instances where I want to do something outside of the build method (Run only once) without changing my state.

Would it be okay to pass a function as a parameter in an Event and inside of the Bloc, run that function after doing something?

For a more specific example, I have data in my ApplicationState that encompasses my whole application. After doing some actions on various pages, my Blocs make API calls to a service and I want to update the data in my ApplicationState in various places throughout my app with the data that I get from the service. I don't feel like the best way to do this is by yielding a new state and I don't know if passing the ApplicationBloc in my Event is good either.

question

Most helpful comment

Closing for now but feel free to comment with more questions/details and we can continue the conversation. 馃憤

All 6 comments

@QoLTech thanks for the question!

Technically you can do that but I wouldn't really recommend it. I'm curious to understand why you are saying you don't believe yielding a new state is the best way to change the application state? I would argue that is the correct way to achieve what you're asking. Can you provide some more details?

Thanks! 馃憤

Well, for example, I need to update something on my server.

  1. Dispatch the Event to the Bloc.
  2. Yield an UpdatingState while awaiting server response.
  3. Server responds with the data I need.
  4. Yield an UpdatedState with the new data?
  5. In my build method, update my ApplicationState data?
  6. Dispatch an Event to the Bloc that I updated the data?
  7. Yield the State I was in before step 1?

I don't want to change the state of my page after the update is basically what I'm held up on because nothing on my page needs to change. My page state is exactly what I want it to be.

  1. Dispatch the Event to the Bloc with a function to callback with the updated data. This callback updates my ApplicationState with the new data to be used app-wide. It isn't necessary to display on this page.
  2. Yield an UpdatingState while awaiting server response.
  3. Server responds with the data I need.
  4. Bloc runs the callback function that I passed in step 1.
  5. Yield the State I was in before step 1.

Does this make sense? I just don't have a need to update my current page state in this process and updating my state basically as a trigger to run a function and then change it back seems unnecessary.

@QoLTech I think I understand what you're trying to do now. Thanks for the extra details 馃憤

I think you don't have to always think of blocs as tying into the UI layer directly. In this case, it might makes sense to have a new bloc for managing the event after the server responds with the data you need. Then, the flow could look something like:

  1. Dispatch the Event to the BlocA.
  2. BlocA yields an UpdatingState while awaiting server response.
  3. Server responds with the data I need.
  4. BlocA yields an UpdatedState with the new data
  5. BlocA dispatches an event to BlocB saying it got new data
  6. BlocB does whatever it needs to do

Let me know if that helps! 馃憤

Okay, I see. So, should I pass in BlocB as a parameter in the Event in step 1? This seems like a better idea, I just wasn't sure if I should have Blocs depend on each other.

I would not need to yield an UpdateState in step 4. I would only need to yield the original state - I don't even need the data back in this page.

I wouldn鈥檛 pass it over the event. I鈥檇 recommend injecting BlocB into BlocA via constructor.

You can see an example of this in the todo tutorial.

Out of curiosity, can you describe the functionality of your app and what this event is doing?

Closing for now but feel free to comment with more questions/details and we can continue the conversation. 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricktotec picture ricktotec  路  3Comments

craiglabenz picture craiglabenz  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments

abinvp picture abinvp  路  3Comments

Reidond picture Reidond  路  3Comments