Home > database >  bloc how should I design it
bloc how should I design it

Time:08-22

I have a bloc which is fetching products using a repository for example products_repository.getAllProducts(). For that reason, in the bloc's state I have declared a List to assign the fetched products.

So far everything works fine.

Now, I want to implement an other bloc event that will fetch a product by id, for example products_repository.getProductById(productId).

My question is should I use a new bloc to implement this? or I can reuse my bloc and add a new event to support it?

CodePudding user response:

I think you should use a new event to do it.

abstract class ProductsEvent {}

//call when handle getAllProducts
class ProductsLoaded extends ProductsEvent {}

//call when handle getProductById
class ProductsByIdLoaded extends ProductsEvent {}

CodePudding user response:

You should use the same bloc and add a new event (even that's always not necessary). Let me explain with an example. Suppose you have a screen that shows a customer quote. Against the customer quote, you show various buttons so that the user can take actions such as:

-----Actions on the document---------

  • Send for approval
  • Cancel
  • Print
  • Convert to a sales order
  • etc

I would create a single event for all these actions, and the event would send a value (enum stating the action user has taken ). I would show the same progress indicator screen till the action is complete. Once the action is completed, I would emit a new state specifying whether the action was a success or failure.

However, I might emit a different event for a separate button (Profitability Analysis). Because now, while the action is in progress, I would want a different message. Like how many old orders I have fetched, what the cost was, etc.

So it all depends on what you want to show on screen. But definitely only one bloc!

  • Related