如何在Flutter中访问和修改任何文件中的变量?

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

How to access and modify a variable in any file in Flutter?

问题

novice in flutter and programming here. It's been few days that I can't find a proper solution to my issue: The thing is I need a variable from my DataWidget, in my MapWidget. But I can't pass it when the widget is called (ex: MapWidget(variable: variable)) because MapWidget isn't called in DataWidget but in main. So I wonder how I could access a variable and modify it from a widget that is at the same level or higher in the widget tree?

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sensors Demo',
      theme: ThemeData(
        useMaterial3: true,
        colorSchemeSeed: const Color(0x9f4376f8),
      ),
      initialRoute: '/',
      routes: {
        '/': (ctx) => DataWidget(),
        MapWidget.routeName: (ctx) => MapWidget()
      },
    );
  }
}

I tried to make it a global variable, but the value doesn't update when modified, so I'm left with the initial value in my other widgets (it is a GpsPositionValue, so it's updating continuously), and I was told global variables aren't the best. I also came across Riverpod but didn't fully understand how to access and modify the value across widgets. I think I need to use StateProviders, but from what I saw, I can only use it in the Widget build? I'm confused about how to use it. Hope my English is clear enough.

英文:

novice in flutter and programming here. It's been few days that I can't find a proper solution to my issue : The thing is I need a variable from my DataWidget, in my MapWidget. But I can't pass it when the widget is called (ex : MapWidget(variable : variable)) because MapWidget isn't called in DataWidget but in main. So I wonder how i could access a variable and modify it from a widget that is same level/higher in the widget Tree ?

class _MyAppState extends State&lt;MyApp&gt; {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: &#39;Sensors Demo&#39;,
      theme: ThemeData(
        useMaterial3: true,
        colorSchemeSeed: const Color(0x9f4376f8),
      ),
      initialRoute: &#39;/&#39;,
      routes: {
        &#39;/&#39;: (ctx) =&gt; DataWidget(),
        MapWidget.routeName: (ctx) =&gt; MapWidget()
      },
    );
  }
}

I tried to make it a global variable but the value doesnt update when modified so im left with the initial value in my other widgets (it is a GpsPositionValue so its updating continuoulsy) and I was told global variable aren't the best.
I also came across riverpod but didn't fully understood how to access and modify the value accross widgets.I think I need to use StateProviders but fromwhat i saw i can only use it in the Widget build ? Im confuse on how to use it. Hope my english is clear enough

答案1

得分: 1

为了从DataWidget类中访问更新后的值,您可以为其创建一个提供程序。

正如您提到的Riverpod,这是获取其引用和更新值的最简单方式。

简单地创建一个返回该实例的提供程序:

final dataWidgetProvider = Provider<DataWidget>((ref) => DataWidget());

将您的StatefulWidget转换为ConsumerStatefulWidget,并将相应的State<MyApp>转换为ConsumerState<MyApp>ConsumerState类会自动获得ref作为属性,因此您可以使用它来获取和监视所需变量的值。

final dataWidget = ref.watch(dataWidgetProvider);

欲了解更多信息,请查阅Riverpod文档:Riverpod文档

英文:

In order to access the updated value from the DataWidget class, you can create a provider for it.

As you mentioned Riverpod, it is the easiest way to get its reference and updated value.

Simply, create a provider that returns the instance:

final dataWidgetProvider = Provider&lt;DataWidget&gt;((ref) =&gt; DataWidget());

Convert your StatefulWidget into ConsumerStatefulWidget and the corresponding State&lt;MyApp&gt; to the ConsumerState&lt;MyApp&gt;. The ConsumerState class automatically gets a ref as a property so you can use it to get and watch the value of your desired variable.

final dataWidget = ref.watch(dataWidgetProvider);

For more, check out Riverpod docs: Riverpod documentation

huangapple
  • 本文由 发表于 2023年6月29日 02:23:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575788.html
匿名

发表评论

匿名网友

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

确定