Bloc: how to use RepositoryProvider

Created on 23 May 2020  路  3Comments  路  Source: felangel/bloc

hi ,thanks for this plugin
i am new to use this
i do not know how to use RepositoryProvider.
could you give me a example of using RepositoryProvider?
i do not know whats difference between RepositoryProvider and blocprovider
.
and i have anthoer problem which is when i use bloc.close()in void dipose() in stateFullwidget . when i close this widget it give me this error:
type 'Future' is not a subtype of type 'Future'

could you please help me ? thank you so much

question

Most helpful comment

Hi @Karatla 馃憢

Please have a look at repository provider documentation for a quick intro.

RepositoryProvider allows you to pass an instance of a repository to a sub-tree from where you can access it via context.repository<YourRepositoryType>().
A typical use case is to inject a repository into a bloc:

RepositoryProvider(
          create: (context) => UserRepository(),
          child: BlocProvider(
            create: (context) => AuthBloc(
              userRepository: context.repository<UserRepository>(),
            ),
            child: ...,
          ),
        )

RepositoryProvider is a specialized widget used to provide a repository while the BlocProvider is a specialized widget used to provide a bloc. As a best practice repositories are injected into blocs and act as an abstraction layer between your data sources and your business logic component(bloc). For more details please have a look at bloc's architecture overview.

As for your error please share a github gist or repo and I'll have a look. 馃憤

All 3 comments

Hi @Karatla 馃憢

Please have a look at repository provider documentation for a quick intro.

RepositoryProvider allows you to pass an instance of a repository to a sub-tree from where you can access it via context.repository<YourRepositoryType>().
A typical use case is to inject a repository into a bloc:

RepositoryProvider(
          create: (context) => UserRepository(),
          child: BlocProvider(
            create: (context) => AuthBloc(
              userRepository: context.repository<UserRepository>(),
            ),
            child: ...,
          ),
        )

RepositoryProvider is a specialized widget used to provide a repository while the BlocProvider is a specialized widget used to provide a bloc. As a best practice repositories are injected into blocs and act as an abstraction layer between your data sources and your business logic component(bloc). For more details please have a look at bloc's architecture overview.

As for your error please share a github gist or repo and I'll have a look. 馃憤

As always, thanks @RollyPeres for taking the time to answer!
@Karatla closing this for now but feel free to comment with additional questions and I'm happy to continue the conversation 馃憤

As far as I am using flutter bloc, I never find a useful way of using the RepositoryProvider, because I instantiate the Repository directly in the bloc constructor. And because I use reverse dependency to abstract the Repository in bloc logic, that means the Repository will have all the required implementation for the specific bloc, and thus makes it a 1-to-1 dependency. So, a specific Repository is used for specific Bloc. That's why I have no idea when and why to use RepositoryProvider. Could you give some practical examples of it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timtraversy picture timtraversy  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

krusek picture krusek  路  3Comments

nerder picture nerder  路  3Comments

ricktotec picture ricktotec  路  3Comments