颤音:从其他提供商那里访问提供商

发布于 2025-01-23 15:08:34 字数 1610 浏览 1 评论 0原文

对于我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2025-01-30 15:08:34

好吧。

因此,同一位作者的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.

未央 2025-01-30 15:08:34

而不是将上下文传递到callfunctionFromSecondProvider函数函数将第二个提供商添加为参数。因此该功能看起来像下面。

不确定这是正确的方法,但是我的上下文无效问题是解决的。

void callFunctionFromSecondProvider({
   required SecondProvider secondProvider,
 }) {

   secondProvider.myFunction();
  }
}

Instead of passing context to the callFunctionFromSecondProvider 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.

void callFunctionFromSecondProvider({
   required SecondProvider secondProvider,
 }) {

   secondProvider.myFunction();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文