0

I have a simple app which 2 tabs, I am trying to you the bloc pattern to move data around.

Flow 1: 1. App starts bloc is created with a seeded value 2. Build method is run for the page and data shows 3. User switches to another tab 4. User switches back, in this case no data is shown as the steam has already be been read once.

One option would be to resubscribe to send the seed value again.

Flow 2: 1. App starts bloc is created with a seeded value 2. Build method is run for the page and data shows 3. User changes the filters - which fires of the stream data is loaded 3. User switches to another tab 4. User switches back, in this case no data is shown as the steam has already be been read once.

In this case, I want the filter to be kept in place and don't want to use the seed value

Bloc

    class DemoBloc {

final DemoApi demoApi;

Stream<String> _results = Stream.empty();
Stream<String> get results => _results;

  BehaviorSubject<String> _tabName =
     BehaviorSubject<String>.seeded('abc');
  // BehaviorSubject<String> _tabName =
  //     BehaviorSubject<String>();

    Sink<String> get tabName => _tabName;  

    DemoBloc(this.demoApi) {
      print('initialized');
      _results = _tabName
        .asyncMap((tab) => tab)
        .asBroadcastStream();

    }

    void dispose() {
    _tabName.close();
  }
}
Asad Ali
  • 151
  • 1
  • 12

1 Answers1

1

As I was using a behaviour subject this shouldn't have been an issue, upon investigation I found that the bloc was being created again in some cases, which resulted in the issue.

How I found

Added a log to the bloc constructor Added a log to bloc provider - logged the caller as well

Result

When I dismissed showSearch - it trigger the build method of the parent which recreated the bloc.

Solution Moved provider to one level above, so build method even if called multiple times won't cause the issue This still didn't resolve it, because I was using a pushReplacement, pushReplacement caused the context to create a new bloc. Finally moved the Provider to the top, so my material app was a child of the provider. As navigator is part of material this resolved the issue even when using pushReplacement.

Asad Ali
  • 151
  • 1
  • 12