颤音:从其他提供商那里访问提供商
对于我的flutter项目,我使用以下多个提供商:
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<FirstProvider>(
create: (context) => FirstProvider(),
),
ChangeNotifierProvider<SecondProvider>(
create: (context) => SecondProvider(),
),
ChangeNotifierProvider<ThirdProvider>(
create: (context) => ThirdProvider(),
),
ChangeNotifierProvider<FourthProvider>(
create: (context) => FourthProvider(),
),
],
child: const MainApp(),
);
}
因为有时我需要从其他提供商那里获取来自其他提供商的数据或调用功能,所以我使用它
//First Provider
class FirstProvider with ChangeNotifier {
void callFunctionFromSecondProvider({
required BuildContext context,
}) {
//Access the SecondProvider
final secondProvider= Provider.of<SecondProvider>(
context,
listen: false,
);
secondProvider.myFunction();
}
}
//Second Provider
class SecondProvider with ChangeNotifier {
bool _currentValue = true;
void myFunction(){
//Do something
}
}
: first -provider
是从小部件调用的,它将成功地调用myFunction()
,大多数时候。,
取决于功能的复杂性,有时我会遇到我无法访问second -provider
,大概是由于上下文
是null
,当widget状态更改时。
我正在网上阅读有关提供商
的一些文档,他们建议changeNotifierProxyProvider
我理解为1至1
提供者关系。
但是,就我而言,一位提供商需要由多个提供商访问,反之亦然。
问题:
是否有一种更合适的方法可以解决我的案件,其中多个提供商可以访问一个提供商?
编辑:
访问提供商还应该能够访问不同的变量值而不创建新实例。
For my flutter project, I am using the following multiple providers below:
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<FirstProvider>(
create: (context) => FirstProvider(),
),
ChangeNotifierProvider<SecondProvider>(
create: (context) => SecondProvider(),
),
ChangeNotifierProvider<ThirdProvider>(
create: (context) => ThirdProvider(),
),
ChangeNotifierProvider<FourthProvider>(
create: (context) => FourthProvider(),
),
],
child: const MainApp(),
);
}
Because sometimes I need to either get data or call functions from different providers from another provider, I am using it like this:
//First Provider
class FirstProvider with ChangeNotifier {
void callFunctionFromSecondProvider({
required BuildContext context,
}) {
//Access the SecondProvider
final secondProvider= Provider.of<SecondProvider>(
context,
listen: false,
);
secondProvider.myFunction();
}
}
//Second Provider
class SecondProvider with ChangeNotifier {
bool _currentValue = true;
void myFunction(){
//Do something
}
}
The callFunctionFromSecondProvider()
of the FirstProvider
is called from a widget and it will call myFunction()
successfully, most of times.
Depending on the complexity of the function, I am sometimes experiencing that I can't access the SecondProvider
, presumably due to context
being null
, when the widget state changes.
I am reading some documents online regarding provider
, and they are suggesting changenotifierproxyprovider
for what I understood as 1 to 1
provider relationship.
However, in my case, one provider needs to be accessed by multiple providers and vice versa.
Question:
Is there a more appropriate way that I can approach my case where one provider can be accessed by multiple providers?
EDIT:
Accessing provider should also be able to access different variable values without creating a new instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧。
因此,同一位作者的Riverpod看起来很重要,因为它解决了许多缺陷,例如提供商依赖于小部件树(在我的情况下)来源的小部件树。
-------------------------------------------------》
仍然需要使用提供商,并且要快速而肮脏的解决方案,我不仅提供了我试图访问提供商的当前小部件的上下文,而且还提供还可以直接传递窗口小部件的父上下文,以防万一(例如)关闭模式(例如),则可以使用父上下文执行任何后续提供者调用。
希望这会有所帮助。
Alright.
So it looks like Riverpod by the same author is the way to go as it addresses alot of flaws such as Provider being dependent on the widget tree, in my case, where the underlying issue came from.
—--------
For the time being, I still need to use the provider and for a quick and dirty solution, I am providing the context of not only the current widget that I am trying to access the provider, but also passing the parent context of the widget directly, so that in case a modal (for example) is closed, then any subsequent provider call can still be executed using the parent context.
Hope this helps.
而不是将
上下文
传递到callfunctionFromSecondProvider
函数函数将第二个提供商添加为参数。因此该功能看起来像下面。不确定这是正确的方法,但是我的上下文无效问题是解决的。
Instead of passing
context
to thecallFunctionFromSecondProvider
function add the second provider as the parameter. So the function looks like the below.Not sure this is the correct way of doing that but my context null issue was fixed this way.