英文:
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<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 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<DataWidget>((ref) => DataWidget());
Convert your StatefulWidget
into ConsumerStatefulWidget
and the corresponding State<MyApp>
to the ConsumerState<MyApp>
. 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论