Skip to content

Help needed: How to use the ProxyProvider and ProxyProvider2? Docs wrong? #279

Description

@talamaska

Now I'm a bit confused. My initial usage of this library is related to including the mobx.
I have seen an example how to use Provider and ProxyProvider to deliver dependencies to the store.
example from the mobx page

providers: [
            Provider<PreferencesService>(
              builder: (_) => PreferencesService(_sharedPreferences),
            ),
            ProxyProvider<PreferencesService, SettingsStore>(
                builder: (_, preferencesService, __) =>
                    SettingsStore(preferencesService)),
          ],
          child: Consumer<SettingsStore>(
            builder: (_, store, __) => Observer(

Now I see that there is no more builder method but instead create method, but It does work differently, as the create method accepts as argument only the context. I inspected the declaration of the ProxyProvider and the create method - it is a ValueBuilder with only context as argument

So this :

ProxyProvider<PreferencesService, SettingsStore>(
                create: (_, preferencesService, __) =>
                    SettingsStore(preferencesService)),

and this example in the readme of this repo

Widget build(BuildContext context) {
  return MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => Counter()),
      ProxyProvider<Counter, Translations>(
        create: (_, counter, __) => Translations(counter.value),
      ),
    ],
    child: Foo(),
  );
}

Are not working as shown.
In addition I need to pass not one but 2 dependencies a Moor AppDatabase instance and a Repository for making some http request. So logically I try to use ProxyProvider2 like that

MultiProvider(
provider: [        
        Provider(create: (_) => FeedRepository()),
        Provider(create: (_) => AppDatabase()),
        ProxyProvider2<AppDatabase, FeedRepository, MainStore>(
          create: (context, db repo) => MainStore(
            db: db,
            feedRepo: repo,
          ),
        )
],
child:...
)

I do see an error
"The argument type 'MainStore Function(BuildContext, dynamic, dynamic)' can't be assigned to the parameter type 'MainStore Function(BuildContext)'."

So the only way I think I can make it work as I need it is

MultiProvider(
      providers: [
        Provider(create: (_) => FeedRepository()),
        Provider(create: (_) => AppDatabase()),
        ProxyProvider2<AppDatabase, FeedRepository, MainStore>(
          create: (context) => MainStore(
            db: Provider.of<AppDatabase>(context),
            feedRepo: Provider.of<FeedRepository>(context),
          ),
        )
      ],
      child:...
)

Could you please help me out with this and confirm that - "this is the way"

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationAn opportunity for improving the documentation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions