skip to Main Content

I am getting into BLOC and I am failing to see the best way to make it possible for my User object to be changed by events coming from multiple BLOCs.

Example – lets say I am building a social media app

AuthBloc contains Sign up / Sign in events and on success emits state with User object which contains some basic data and list of posts.

Home screen reads that state and shows data from the User object.

Problem:

As part of different BLOC – lets say PostBloc – there are events related to CRUD actions for user’s posts.

Each of these events changes User object’s "posts" List and therefor it returns a new User object with updated list of posts.

This creates a problem because state of User returned from AuthBloc is old and no longer relevant because PostBloc’s state returns User with same data as AuthBloc + new posts list.

What is best approach here? I tried looking up different examples as well as official Bloc examples but none of them seem to be very complete – they all like cover basic authentication flow but never actually proceed further with how to manage the user state afterwards.

Ideally I would like to make it so each Bloc can update user object’s properties but the I am failing to see approach for this if we consider that everything should cause Bloc event and event should emit new State.

2

Answers


  1. on BLOC there are 2 states.

    https://bloclibrary.dev/#/blocnamingconventions:~:text=%E2%9C%85-,Good,-sealed%20class%20CounterEvent

    i think you should update the Users data on Userbloc state too.
    i recommend you to use the SingleClass state

    Login or Signup to reply.
  2. Take a look at this article, hope it would help you:

    The Observer Pattern: Facilitating Bloc Dependency Management in Dart

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search