felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

What is the difference usage Between BlocProvider and RepositoryProvider ? #2477

Closed drogbut closed 3 years ago

robsonsilv4 commented 3 years ago

BlocProvider is used to provide Bloc and Cubit (Stream and State) and RepositoryProvider is used to provide another classes (repositories, data providers and etc), like normal Provider.

felangel commented 3 years ago

Hi @drogbut 👋 Thanks for opening an issue!

As @robsonsilv4 mentioned, BlocProvider is used to provide a single instance of a bloc or cubit to the widget tree. It handles automatically closing the bloc/cubit when the subtree is unmounted so you don't have to. Similarly, RepositoryProvider is used to provide a single instance of a repository to the widget tree. I highly recommend reading through the architecture documentation if you haven't already and you can take a look at BlocProvider and RepositoryProvider in action in various examples such as the weather example. Hope that helps!

Closing for now but feel free to comment with any additional questions or join us on discord 👍

drogbut commented 3 years ago

The definition is clear but in the implementation with Firebase I have trouble understanding. Indeed the RepositoryProvider could be passed to the bloc in its constructor. To me this seems to be the right approach.

My question is whether the best approach is to pass the Repository into the constructor of the bloc or to wrap the bloc in the Repository?

drogbut commented 3 years ago

BlocProvider is used to provide Bloc and Cubit (Stream and State) and RepositoryProvider is used to provide another classes (repositories, data providers and etc), like normal Provider.

My question is whether the best approach is to pass the Repository into the bloc-constructor or to wrap the block in the Repository?

Pingear commented 2 years ago

BlocProvider is used to provide Bloc and Cubit (Stream and State) and RepositoryProvider is used to provide another classes (repositories, data providers and etc), like normal Provider.

My question is whether the best approach is to pass the Repository into the bloc-constructor or to wrap the block in the Repository?

@drogbut @felangel Hi) I have the same misunderstanding, which approach is better? Did you find the answer?

felangel commented 2 years ago

BlocProvider is used to provide Bloc and Cubit (Stream and State) and RepositoryProvider is used to provide another classes (repositories, data providers and etc), like normal Provider.

My question is whether the best approach is to pass the Repository into the bloc-constructor or to wrap the block in the Repository?

@drogbut @felangel Hi) I have the same misunderstanding, which approach is better? Did you find the answer?

I recommend providing your repositories to the widget tree using RepositoryProvider and then injecting the provided instance into the bloc:

BlocProvider(
  create: (context) => MyBloc(context.read<MyRepository>()),
  child: MyChild(),
)

Hope that helps 👍

Pingear commented 2 years ago

BlocProvider is used to provide Bloc and Cubit (Stream and State) and RepositoryProvider is used to provide another classes (repositories, data providers and etc), like normal Provider.

My question is whether the best approach is to pass the Repository into the bloc-constructor or to wrap the block in the Repository?

@drogbut @felangel Hi) I have the same misunderstanding, which approach is better? Did you find the answer?

I recommend providing your repositories to the widget tree using RepositoryProvider and then injecting the provided instance into the bloc:

BlocProvider(
  create: (context) => MyBloc(context.read<MyRepository>()),
  child: MyChild(),
)

Hope that helps 👍

Thanks for the answer) I'm ecstatic about your library, awesome features and documentation, thanks)