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

Multiple blocs for better organization #5

Closed
israelazo opened this issue Apr 8, 2020 · 2 comments
Closed

Multiple blocs for better organization #5

israelazo opened this issue Apr 8, 2020 · 2 comments

Comments

@israelazo
Copy link

Hi Riberto.

I'm pretty new to flutter and this generic bloc provider works really well so far. I'm trying to organize my code into classes and different blocs, but i can't find a way to pass multiple blocs to my main class or widgets.

Something like:

final userBloc = BlocProvider.of<UserBloc>(context);
final productBloc = BlocProvider.of<ProductBloc>(context);

Do you think is possible?

@robertohuertasm
Copy link
Owner

robertohuertasm commented Apr 8, 2020

Hi @israelazo, I'm not really sure to understand your problem but if you want to get access to several blocs in one widget your approach should work.

It's also usual to compose blocs. For instance, you may have a top level bloc called appBloc which contains an instance of several specific blocs.

Another thing I like to do is to provide a helper class with static methods to avoid being verbose when accessing differents blocs:

import 'package:generic_bloc_provider/generic_bloc_provider.dart' as provider

class BlocProvider {
  static AppBloc of(BuildContext context) => appBloc(context); //alias

  static AppBloc appBloc(BuildContext context) =>
      provider.BlocProvider.of<AppBloc>(context);

  static ContentBloc userBloc(BuildContext context) =>
      appBloc(context).userBloc;

  static ProfileBloc productBloc(BuildContext context) =>
      appBloc(context).productBloc;
}

Not sure if I answered your question but feel free to ask if not 😁

@israelazo
Copy link
Author

Thank you, it wass exaclty that!
this wasn't really an issue, but I couldn't find another way to ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants