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

Provider Extensions #841

Open
onatcipli opened this issue Oct 19, 2023 · 1 comment
Open

Provider Extensions #841

onatcipli opened this issue Oct 19, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@onatcipli
Copy link

Is your feature request related to a problem? Please describe.
While working with providers in Flutter, I've observed that the current methods, though effective, can sometimes lead to boilerplate code. This repetition can hinder readability and maintainability, especially in larger projects.

Describe the solution you'd like
I've opened a pull request (#840) that introduces Provider Extensions to the Widget class. These extensions are designed to offer alternative syntactic sugar that reduces boilerplate, providing a more streamlined approach to handling providers. Here are the specifics:

ProviderExtension

  provideProvider: Acts as an equivalent for Provider.
  provideProviderValue: Acts as an equivalent for Provider.value.

MultiProviderExtension

  provideMultiProvider: Acts as an equivalent for MultiProvider.

ChangeNotifierProviderExtension

  provideChangeNotifierProvider: Acts as an equivalent for ChangeNotifierProvider.
  provideChangeNotifierProviderValue: Acts as an equivalent for ChangeNotifierProvider.value.

Example:

Traditionally, you would use:

Provider<MyObject>(
  create: (_) => MyObject(),
  child: MyWidget(),
);

With the proposed ProviderExtension, it can be written as:

MyWidget().provideProvider(
  create: (_) => MyObject(),
);

Describe alternatives you've considered
The existing methods for providing providers have been effective and are widely used.

The proposed extensions are not meant to replace them but to complement them, giving developers an additional choice in how they structure their code. Developers can continue using the traditional methods or opt for the proposed extensions based on their preference and the specific scenario.

Additional context
These extensions aim to simplify and enhance the developer experience when working with providers in Flutter. By reducing boilerplate, we can increase code readability and maintainability.

@rrousselGit
Copy link
Owner

I'm personally not fond of the idea.

This is not something Flutter generally does. And to begin with, providers are used inside MultiProvider nowadays. We rarely use a provider on its own.
It also creates more competing syntaxes for the same thing.

If using extensions for single-child widgets was a common practice, I wouldn't mind. But that's not the case.

At the same time, this is something which can easily be done on your own. It doesn't have to be inside Provider itself to be doable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants