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

Allow to inject multiple PropertyChangeProvider at the root of app #5

Open
trietbui85 opened this issue Sep 12, 2019 · 19 comments
Open

Comments

@trietbui85
Copy link

I see in provider package, they allow us to inject multiple Provider at the root of app by using MultiProvider to avoid nested Provider, like this:

@override
Widget build(BuildContext context) {
  return MultiProvider(
	providers: [
	  ChangeNotifierProvider(builder: () => homeBloc),
	  ChangeNotifierProvider(builder: () => maintainBloc),
	],
	child: MaterialApp(
	  title: 'Flutter Demo',
	  home: LandingPage()
	),
  );
}

So, could you also add an equivalent MultiPropertyChangeProvider too?

@martinrybak
Copy link
Owner

Nice! It shouldn't be too hard. To use PropertyChangeProvider with MultiProvider, all you have to do is make it implement SingleChildCloneableWidget. However that would put a dependency from this library on the Provider package. Not sure if that's a good idea.

@martinrybak
Copy link
Owner

See the multiprovider branch for a working example.

@trietbui85
Copy link
Author

Hi @martinrybak . Thanks for that working sample. It seems not so hard to implement PropertyChangeProvider with MultiProvider. But as you said, not sure it's a good idea to include provider dependency.
Thus, I think about another approach: implement your own MultiPropertyChangeProvider instead. Is it difficult to implement that?

@martinrybak
Copy link
Owner

Probably not, but I also don't want to go down the slippery slope of duplicating other Provider package features...

@trietbui85
Copy link
Author

Ok, I understand. So now, it's up to you to release new version of PropertyChangeProvider with provider dependency.

@martinrybak
Copy link
Owner

Do you think it's a good idea to depend on Provider? I was struggling with that question as I made this library.

@anlam87
Copy link

anlam87 commented Sep 13, 2019

Hi @martinrybak , I'm using Provider + ChangeNotifier + StreamBuilder for my app. But since having your package, I'm considering to change to your lib because it solves some problems as you mentioned. However, from my real projects, MultiProvider is really necessary. It's better if the lib supports MultiProvider.
Depending on the Provider is not a bad idea because of the popular Provider. The Provider is recommended by the Google Flutter team, so the community will trust your lib if it bases on Provider.
However, you only need to use a very small part on Provider (by implementing SingleChildCloneableWidget). In my opinion, you should implement yourself. Duplicating is not important, as long as you can make a good library for yourself and for the community

@martinrybak martinrybak reopened this Sep 13, 2019
@trietbui85
Copy link
Author

IMHO, your property_change_notifier looks promising. And if I decide to use it, it would replace provider. Thus, in my (happy case), I won't have to include both property_change_notifier and provider dependencies in project.

@chimon2000
Copy link

I don't see an issue with depending on Provider if there is some functionality that provides value or reduces duplication, such as multi-provider. But if that is a major concern why not have a separate package for that?

@rrousselGit
Copy link

What about having provider use InheritedModel?

provider won't make a PropertyChangeNotifier, but it could support "aspects", which would allow a package like this one to have better integration.

What do you think?

@trietbui85
Copy link
Author

Here is my workaround to make code shorter when using multiple PropertyChangeProvider

Widget tree = MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
 );
tree = PropertyChangeProvider(value: MyModel1(), child: tree);
tree = PropertyChangeProvider(value: MyModel2(), child: tree);
... 
return tree;

@martinrybak
Copy link
Owner

@rrousselGit Thanks for weighing in! Supporting aspects in Provider definitely opens the door to using it with PropertyChangeNotifer models. The tricky detail is that PropertyChangeProvider doesn't return the model directly from its of method. It returns a type that contains both the model and the changed property (aspect). That is different than the way Provider works now.

@rrousselGit
Copy link

It's technically feasible to expose the latest aspects changed. But before that, what's the use-case?

@martinrybak
Copy link
Owner

One of the features of PropertyChangeNotifier is that you can observe multiple properties with the same listener and it will tell you which one changed. However I think this effect can be achieved by simply using multiple Consumer widgets. Does anybody on this thread have a use case that could not be solved in this manner?

@KevinRohn
Copy link

Is there any update on this to use the PropertyChangeProvider with MultiProvider?

@worawutt
Copy link

See the multiprovider branch for a working example.

Note @martinrybak provider v4.0.0
The SingleChildCloneableWidget interface is removed and replaced by a new kind of widget SingleChildWidget.

@sjapps
Copy link

sjapps commented May 16, 2020

So I've gone down the path that everyone here probably has. I was looking at ChangeNotifier and started implementing. Awesome idea and awesome library. As your models grow, you begin to realize that you are updating the whole model with the smallest changes. Then you do some googling and find this awesome library (thank you). But then you run into this blocker because your code is passing in multiple providers (since you like to think seperation of concern and have providers broken down).

IMO it is okay to depend on the provider package. Any medium size project will probably use that in some ways. As long as we can contain the import, I think we should be fine.

@stukdev
Copy link

stukdev commented Oct 11, 2021

The same for me, now i'm blocked because i can't use with MultipleProvider.

@saarangtiwari
Copy link

Any updates on this issue as I am also stuck because of not being able to use MultipleProvider?

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

10 participants