适当地访问消费者扑朔迷离
我无法从 Consumer
访问我的 ChangeNotifierProvider
但我不明白为什么,与提供程序相比,它在小部件树中定义得更深。
我在这里定义了我的提供程序:
body: GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: ChangeNotifierProvider<QuestionProvider>(
//TODO : question provider arguments should come from internet
create: (newContext) => QuestionProvider([], pageController),
child: Column(
children: [
TypeAndSettingsRow(),
消费者在 TypeAndSettingsRow
中定义的下一个小部件中定义:
class TypeAndSettingsRow extends StatefulWidget {
@override
State<TypeAndSettingsRow> createState() => _TypeAndSettingsRowState();
}
class _TypeAndSettingsRowState extends State<TypeAndSettingsRow> {
@override
Widget build(BuildContext context) {
void showPopoverType() {
int thisIsTrue = 0;
double size = MediaQuery.of(context).size.width * 0.9;
showPopover(
context: context,
tion(milliseconds: 150),
bodyBuilder: (context) => CheckboxListTileRow(),
最后 CheckboxListTileRow
:
class _CheckboxListTileRowState extends State<CheckboxListTileRow> {
@override
Widget build(BuildContext context) {
return Consumer<QuestionProvider>(builder: ((context, value, child) {
QuestionProvider
:
class QuestionProvider extends ChangeNotifier {
当我打开 CheckboxListTileRow
这是错误:
Error: Could not find the correct Provider<QuestionProvider> above this Consumer<QuestionProvider> Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that Consumer<QuestionProvider> is under your MultiProvider/Provider<QuestionProvider>.
我不明白哪个是问题,因为我的提供者是在其消费者的小部件树方面定义的,因此它应该是可访问的。
也许context
有一些错误?
编辑:
发现问题是 showPopover
形式 popover 包,当我尝试在 showPopover
内使用 Consumer
访问 ChangeNotifierProvider
时,会抛出此错误,但如果我在外部执行此操作,则它会起作用。 可能它创建了一条新路线,我无法再访问我的提供商。 希望这个新提示可以帮助解决,因为问题仍然悬而未决。
I can't access my ChangeNotifierProvider<QuestionProvider>
from Consumer<QuestionProvider>
but I don't understand why, it's defined deeper in the widget tree compared to its provider.
I defined my provider here :
body: GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: ChangeNotifierProvider<QuestionProvider>(
//TODO : question provider arguments should come from internet
create: (newContext) => QuestionProvider([], pageController),
child: Column(
children: [
TypeAndSettingsRow(),
Consumer is defined in a next widget defined inside TypeAndSettingsRow
:
class TypeAndSettingsRow extends StatefulWidget {
@override
State<TypeAndSettingsRow> createState() => _TypeAndSettingsRowState();
}
class _TypeAndSettingsRowState extends State<TypeAndSettingsRow> {
@override
Widget build(BuildContext context) {
void showPopoverType() {
int thisIsTrue = 0;
double size = MediaQuery.of(context).size.width * 0.9;
showPopover(
context: context,
tion(milliseconds: 150),
bodyBuilder: (context) => CheckboxListTileRow(),
And finally CheckboxListTileRow
:
class _CheckboxListTileRowState extends State<CheckboxListTileRow> {
@override
Widget build(BuildContext context) {
return Consumer<QuestionProvider>(builder: ((context, value, child) {
QuestionProvider
:
class QuestionProvider extends ChangeNotifier {
When I open CheckboxListTileRow
this is the error :
Error: Could not find the correct Provider<QuestionProvider> above this Consumer<QuestionProvider> Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that Consumer<QuestionProvider> is under your MultiProvider/Provider<QuestionProvider>.
I don't understand which is the problem because my provider is defined above in the widget tree respect of its consumer so it should be reachable.
Maybe there are some errors with context
s?
EDIT :
Found out that the problem is showPopover
form popover package, when I try to access ChangeNotifierProvider
with Consumer
within showPopover
this error is thrown but if I do it outside then it works.
Probably it creates a new route and I can't access my provider anymore.
Hope this new hint can help to fix because question is still open.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这意味着您尚未在
main.dart
文件中提到提供商类。为此,您必须使用以下代码。
如果您有多个提供商,则可以使用以下代码。
This means you have not mentioned the provider class in your
main.dart
file.For that, you have to use the following code.
If you have multiple providers you can use the following code.
你为什么使用
changeNotifierProvider 在您的小部件树的根上?如果您不需要它,则可以在小部件树中使用它并使用消费者,因为它在儿童上不是另一个小部件
why did you use
changeNotifierProvider on root of your widget tree? if you haven't need to like that you can use it where ever in your widget tree and use consumer as it's on child not another widget