0

I am trying to set all dependencies using Provider and not using get_it or other kind of instantiation in initState

return MultiProvider(
providers: [ 
    Provider<UserRepository>.value(value: FbUserRepository()),            
    ChangeNotifierProvider<AppModel>(builder: (context) => AppModel()),
    ChangeNotifierProvider<AuthModel>(builder: (context) => AuthModel()),  
    ProxyProvider3<UserRepository, AuthModel, AppModel, AppController>(
         updateShouldNotify: (_, __) => false,       
         initialBuilder: (_) => AppController(),       
         builder: (_, repo, authModel, appModel, app) => app..model = appModel ..userRepository = repo, 
), ],

The AppController(or any other viewModel used in some section of the app) has methods that will change the AppModel. Changes in AppModel will reflect in different places on the app.

How I can call some start()/init() method on AppController when on all the dependencies are set? (I cant use the AppController constructor to pass the dependencies, as they(the dependencies) are not passed on initialBuilder).

cosinus
  • 1,905
  • 3
  • 15
  • 21
  • What do you mean by "all dependencies are set"? – Rémi Rousselet Sep 17 '19 at 10:51
  • It means that AppViewModel has set the appModel, repo and the otherModel properties. When this properties are set(by ProxyProvider) then I can call some async init/start method. Until now I was creating all the instances and dependencies on initState() and passed them to Provider in build method. Also from initState I was calling _appViewModel.init(). – cosinus Sep 17 '19 at 11:56
  • What about a custom setter for fields like `AppViewModel.model`, that calls the method if needed – Rémi Rousselet Sep 17 '19 at 12:16
  • AppViewModel needs all the dependencies to be defined so it can run the init() method. I think I will rename the AppViewModel in AppController. Before using ProxyProvider, as I comment earlier, I used initState() for setting all up and call init() but now I want to move all of this in ProxyProvider – cosinus Sep 17 '19 at 12:44
  • @RémiRousselet is ok to call some async method (does call some http, etc, will do changes on AppModel) on the builder method of AppController(ProxyProvider)? – cosinus Sep 17 '19 at 14:04
  • Yes, but bear in mind that tbe method could be called again any time. So you have to check that you're not doing the request again for no reason – Rémi Rousselet Sep 17 '19 at 14:16
  • @RémiRousselet my architecture is that the controllers are changing the models, models are ChangeNotifierProvider to the views. The controllers are ProxyProvider to a whole page/section. In the ProxyProvider builder method the controller gets all the dependencies needed. Also the controller is only for calling use cases, only models are changing the views (models chan be shared using ProxyProvider in other controllers). For some controllers when all dependencies are defined, start() or init() should be called. – cosinus Sep 17 '19 at 15:25
  • @RémiRousselet, in the end after some modifications, etc, I have something like `builder: (_, userRepository, authVm, appVm, app) { app.setDependencies(userRepository, authVm, appVm); app.init(); /*runs once*/ return app; })` – cosinus Sep 17 '19 at 21:20

0 Answers0