[英]MultiProvider executing function Flutter
i have Text in GestrureDetector widget, i want to execute my function showNameAction by clicking on text "imie".我在GestrureDetector小部件中有文本,我想通过单击文本“imie”来执行我的 function showNameAction 。
Error:错误:
" Could not find the correct Provider above this FriendsPageEditable Widget " “在此 FriendsPageEditable 小部件上方找不到正确的提供者”
FriendsEditablePage.dart: (short version) (stateless class) FriendsEditablePage.dart:(短版) (无状态类)
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: SaveBoxes()),
],
child: Scaffold(
body: GestureDetector(
onTap: () {
Provider.of<SaveBoxes>(context, listen: false).showNameAction;
},
child: Text(imie))),
SaveBoxes.dart SaveBoxes.dart
class SaveBoxes extends ChangeNotifier {
void showNameAction(BuildContext context) {
showDialog<void>(
context: context,
builder: (context) {
return AlertDialog();
},
);
}
}
This line of code looks not right to me:这行代码对我来说看起来不对:
ChangeNotifierProvider.value(value: SaveBoxes()),
You are using ChangeNotifierProvider.value
widget which is used when you already have an instance and don't want create a new one.您正在使用
ChangeNotifierProvider.value
小部件,当您已经有一个实例并且不想创建一个新实例时使用该小部件。 But in value: SaveBoxes()
you create an instance.但在
value: SaveBoxes()
您创建了一个实例。 So try changing it to:所以尝试将其更改为:
ChangeNotifierProvider(create: (context) => SaveBoxes()),
You can use Multiprovider
in main.dart and call SaveBoxes saveBoxes = Provider.of<SaveBoxes>(context)
in friendseditablepage.dart您可以在 main.dart 中使用
Multiprovider
并在friendseditablepage.dart中调用SaveBoxes saveBoxes = Provider.of<SaveBoxes>(context)
)
This is the source code based on your case.这是基于您的案例的源代码。 Hope it work:
希望它有效:
main.dart file: main.dart文件:
void main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => SaveBoxes(),
),
],
child: const MaterialApp(
home: FriendsEditablePage(),
),
);
}
}
friendseditablepage.dart file: Friendseditablepage.dart文件:
class FriendsEditablePage extends StatelessWidget {
const FriendsEditablePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
SaveBoxes saveBoxes = Provider.of<SaveBoxes>(context);
return Scaffold(
body: SafeArea(
child: GestureDetector(
onTap: () {
saveBoxes.showNameAction(context);
},
child: const Text('imie'),
),
),
);
}
}
saveboxes.dart file: saveboxes.dart文件:
class SaveBoxes extends ChangeNotifier {
void showNameAction(BuildContext context) {
showDialog<void>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: const <Widget>[
Text('This is a demo alert dialog.'),
Text('Would you like to approve of this message?'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Approve'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
Try to wrap your GestureDetector
inside a Builder
so you can access the updated context.尝试将您的
GestureDetector
包装在Builder
中,以便您可以访问更新的上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.