Search code examples
flutterbloc

Flutter bloc data sharing with an update subscription


I have Bloc A and Bloc B and I need each of these blocs to have access to the List<String> Items. This is easy to organize, it is enough to create a Repository class, in which to place this List<String> Items and pass it to each of the blocs when they are created. The problem is that when updating the data in the repository from bloc A, bloc B does not know that the repository data has changed. Perhaps I did not understand and read the documentation poorly, but I did not find an answer to my problem there. How can I subscribe to changes in data from several blocks at once?


Solution

  • There are several options, some are:

    1. Common repository that expose methods providing streams to subscribe to in each bloc.
    2. Connect blocs A and B via UI using BlocListeners and events.

    Official documentation describes bloc to bloc communication here, which this is.

    https://bloclibrary.dev/#/architecture?id=bloc-to-bloc-communication