admin管理员组

文章数量:1200976

I am creating a flutter page that uses google maps as a base, and has a search bar on top. Both components interact with the list of markers I use to populate the map, but trying to manipulate the list using callback functions is like pulling out teeth. I read that if I was trying to manipulate complex states like this, I should use state management packages like Riverpod, but reading about it makes me think that providers are like global variables? Except I don't want my list of Markers to be available to all my pages, just my main page and its child components.

The documentation for riverpod is frustrating, everything is either out of date, or really simple. The only thing that I found that was similar was scoping my providers, but that just means having a hanging provider around that is doing nothing. Is there another way to do this or am I looking at it the wrong way?

I am creating a flutter page that uses google maps as a base, and has a search bar on top. Both components interact with the list of markers I use to populate the map, but trying to manipulate the list using callback functions is like pulling out teeth. I read that if I was trying to manipulate complex states like this, I should use state management packages like Riverpod, but reading about it makes me think that providers are like global variables? Except I don't want my list of Markers to be available to all my pages, just my main page and its child components.

The documentation for riverpod is frustrating, everything is either out of date, or really simple. The only thing that I found that was similar was scoping my providers, but that just means having a hanging provider around that is doing nothing. Is there another way to do this or am I looking at it the wrong way?

Share Improve this question asked Jan 22 at 6:52 P.HuangP.Huang 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This is a common misconception. Access to a provider really requires three things... the unique provider (usually but not always stored in a global variable), a "ref" (typically obtained via the context chain), and a ProviderContainer (usually enclosed in the nearest ProviderScope, which can be nested. Family providers add a fourth level of distinction in the family key.

So providers are hardly "global", because you need the right 3 or 4 values to effectively locate a specific provider.

I illustrate this in my video: https://www.youtube.com/watch?v=gHbkIgDnDyg

本文标签: flutterHow to use Riverpod Providers as a SemiGlobal VariablesStack Overflow