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

[feature]: Shared ViewModel Across PageView Descendants #1127

Open
xeinebiu opened this issue Sep 17, 2024 · 1 comment
Open

[feature]: Shared ViewModel Across PageView Descendants #1127

xeinebiu opened this issue Sep 17, 2024 · 1 comment
Labels
Feedback Requested Waiting for new information from the OP

Comments

@xeinebiu
Copy link

xeinebiu commented Sep 17, 2024

Is your feature request related to a problem? Please describe.

In my use case, I want to share state between each PageView children, similar to how ActivityViewModel works in native apps. Currently, there is no built-in way to share a ViewModel between a parent widget like PageView and its child widgets, making communication and state management between pages difficult.

Describe the solution you would like

I would like a way for the PageView's ViewModel to be shared with its descdendants widgets (pages) so they can communicate and share state easily.

Alternatively, a service that lives as long as a specific widget exists could also solve this problem. This is similar to how dependency injection works in native development using Hilt. For example, in native Android, you can scope a service to an Activity, Fragment, or NavigationGraph using Hilt, and the service will live as long as that scope exists.

In Flutter, this could translate to a service scoped to a specific widget's lifecycle, that it is created and destroyed with that widget. This would allow for managing shared state between pages or routes, for example, using GoRouter for navigation or widgets with a well-defined lifecycle.

Additional Context

I expect ViewModels to behave similarly to how Providers work in many state management systems, such as Native Android or React Contexts. I'm not sure why Stacked enforces the rule of "1 view, 1 view model" when other systems allow descendants to access higher scope Providers or ViewModels.

@FilledStacks
Copy link
Contributor

Hey,

This is solved through services.

Put the state in a shared service, then read the state in the viewmodel that you want.

ViewModels is for managing the UI state of the specific view on screen. By making it strictly 1-to-1, it's very hard to create spaghetti code.

And since stacked is built for maintainability and testability, allowing the fetching of viewModels within other viewModels will make it hard to stick to that rule.

@FilledStacks FilledStacks added the Feedback Requested Waiting for new information from the OP label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feedback Requested Waiting for new information from the OP
Projects
None yet
Development

No branches or pull requests

2 participants