-
-
Notifications
You must be signed in to change notification settings - Fork 514
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.shared for Efficient SharedInstance Management #872
Comments
Honestly I'll admit that I'd like to refrain from adding new APIs to Provider. I'm not fond of the proposed Also, another challenge could be that two routes may define the a In Riverpod, the way it solves this problem is by still defining providers above |
I understand the concerns regarding adding a new API like However, introducing This enhancement to the |
We could also consider adding a separate class, such as SharedProvider, to encapsulate this functionality |
First of, I assume folks would be able to implement this without any change to pkg:provider. Am I right? |
Is your feature request related to a problem? Please describe.
During the development of mobile applications using the Flutter framework, a common task is to organize efficient navigation between screens and manage the application state effectively. A particularly pressing issue arises when there is a need to transfer and reuse specific instances for state management, services, or data across different screens. For instance, consider a scenario where we have two screens:
ScreenA
andScreenB
, between which we need to pass and utilize the same instance for state management.A traditional approach involves creating and initializing instances at a level that is above all screens, such as in the global state of the application or above
MaterialApp
. This allows instances to be accessible anywhere in the application. However, this approach has a significant drawback: the instance remains in memory for the entire lifecycle of the application, even when not in use. This leads to inefficient resource usage, especially in cases where the instance is only needed for a short period or in a limited context.An alternative approach, which involves directly passing instances between screens via navigation arguments, also has its limitations. Firstly, it can be cumbersome when working with complex data structures or a large amount of data that needs to be passed. Secondly, with the use of declarative navigation and routing, this approach can complicate the navigation architecture, making the code less readable and maintainable.
Developers thus face a dilemma: how to ensure efficient reuse of instances between screens without unnecessarily loading memory and complicating navigation logic. The solution should offer a flexible mechanism for managing the lifecycle of instances, allowing them to be created as needed and resources to be freed when they are no longer necessary, thereby optimizing memory usage and improving the performance of the application.
Passing data between screens via navigation arguments is limited and not always suitable for working with complex data structures or in the context of declarative navigation.
Describe the solution you'd like
To enhance the capabilities of
provider
in Flutter, it is proposed to add functionality that allows providers to manage shared instances, such as state objects or services:Centralized Repository: Implement a repository for shared instances so that providers can facilitate access to them.
Instance Reuse: Providers should check for the presence of a required instance in the repository and reuse it if available. If the instance is absent, the provider creates a new one and adds it to the repository.
Lifecycle Management: Providers automatically remove instances from the repository when they are no longer in use.
These improvements will enable more efficient resource use and simplify working with shared instances in the application.
Describe alternatives you've considered
Considered alternatives:
Direct instance transfer between screens: Directly passing instances through arguments during screen transitions. This method is simple but becomes inconvenient with an increase in the amount of data.
Global instances: Using global variables or singletons for instance access. Easy to implement but can lead to memory management issues.
Provider
aboveMaterialApp
: Placingprovider
at the top level of the widget tree for global instance access. Ensures availability but doesn't address the issue of inefficient memory use.Additional context
The text was updated successfully, but these errors were encountered: