传递动态初始值给 Riverpod 提供者

huangapple go评论54阅读模式
英文:

Passing dynamic initial value to riverpod provider

问题

我有一个如下的用例:一个带有初始筛选对象的屏幕,这个屏幕多次使用不同的筛选配置,我需要实现的是从这个屏幕传递一个不同的初始筛选对象给 riverpod 通知提供程序,然后我可以使用提供程序更改此筛选的状态,我该如何做?

我尝试将筛选对象传递给 build 方法,但这并没有起作用。

英文:

I have a usecase as follow: a screen with initial filter object, this screen is used multiple times with different filter configuration, what I need to achieve is to pass an initial different filter object for each instance from this screen to riverpod notifier provider then I can change the state of this filter using the provider, how I can do that?

I have tried to pass a filter object to the build method but this didn't work.

答案1

得分: 1

你需要决定是每次只想要一个过滤器提供者,还是想要许多拥有自己过滤器的提供者。如果是后者,请将关于过滤器的信息作为一个家庭键传递进去。如果是前者,那么提供者的构建方法可以消耗(ref.watch)另一个持有过滤器的提供者。该过滤器持有者可以通过其通知器上的变异方法进行更新。

英文:

You need to decide whether you want only one filter for that provider at a time, or whether you want many providers with their own filters. If the latter, pass something about the filter in as a family key. If the first, then have the build method of the provider consume (ref.watch) another provider that holds the filter. That filter-holder can then be updated via mutate methods on its notifier.

huangapple
  • 本文由 发表于 2023年6月6日 04:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定