Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining 2 Bloc state in a new Bloc #376

Closed
anatter opened this issue Jun 25, 2019 · 6 comments
Closed

Combining 2 Bloc state in a new Bloc #376

anatter opened this issue Jun 25, 2019 · 6 comments
Assignees
Labels
bug Something isn't working
Projects

Comments

@anatter
Copy link
Contributor

anatter commented Jun 25, 2019

Hello,

what is best practice for combing multiple Bloc state in a new Bloc? For example I have BlocA and BlocB and I want to create a new Bloc which combines both states:

class BlocCombineBloc extends Bloc<BlocCombineEvent, BlocCombineState> {
  final BlocABloc blocA;
  final BlocBBloc blocB;

  BlocCombineBloc(this.blocA, this.blocB);

  @override
  BlocCombineState get initialState => InitialBlocCombineState();

  @override
  Stream<BlocCombineState> mapEventToState(
    BlocCombineEvent event,
  ) async* {
    Observable.combineLatest2(blocA.state, blocB.state,
        (BlocAState stateA, BlocBState stateB) {
      if (stateA is BlocALoaded && stateB is BlocBLoaded) {
        /// combine data
      }
    }).listen((data) {
      /// do something with data
    });
  }
}

The problem which I am facing is when my BlocCombineBloc calls disposed(), BlocA and BlocB call also disposed().

@arok
Copy link

arok commented Jun 25, 2019

I have the same case but fix it from other side. With the following CombineLatestBlocBuilder

@anatter
Copy link
Contributor Author

anatter commented Jun 25, 2019

I made a sample application with my problem: https://github.com/anatter/flutter_bloc_dispose
I have no Idea why BlocA and BlocB call dispose...

@filoe
Copy link

filoe commented Jun 25, 2019

Don't really know the package but I would guess the dispose parameter is missing here:
https://github.com/felangel/bloc/blob/master/packages/flutter_bloc/lib/src/bloc_provider.dart#L72
Can't really tell whether there is any reason to exclude the dispose parameter. If so, there has to be an alternate solution.
What would u guess @felangel

@anatter
Copy link
Contributor Author

anatter commented Jun 25, 2019

Don't really know the package but I would guess the dispose parameter is missing here:
https://github.com/felangel/bloc/blob/master/packages/flutter_bloc/lib/src/bloc_provider.dart#L72
Can't really tell whether there is any reason to exclude the dispose parameter. If so, there has to be an alternate solution.
What would u guess @felangel

Thanks @filoe, this resolved the issue 😆 🎉

@felangel I opened a Pull request: #377

@felangel felangel self-assigned this Jun 25, 2019
@felangel felangel added bug Something isn't working in progress labels Jun 25, 2019
@felangel
Copy link
Owner

Hey @anatter 👋
Thanks for opening an issue and for the PR!

@filoe thanks for catching that! I must’ve missed it during the recent refactor. Will merge the PR shortly and include the fix in flutter_bloc v0.18.3.

Thanks again everyone! 🙏

@felangel
Copy link
Owner

Fix merged in #377 and included in flutter_bloc v0.18.3 🎉
Thanks everyone! 💯

@felangel felangel added this to Done in bloc Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
bloc
  
Done
Development

No branches or pull requests

4 participants