适当地访问消费者扑朔迷离

发布于 2025-01-20 06:50:57 字数 2877 浏览 3 评论 0原文

我无法从 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 contexts?

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 技术交流群。

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

发布评论

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

评论(2

迟月 2025-01-27 06:50:57
 Make sure that Consumer<QuestionProvider> is under your MultiProvider/Provider<QuestionProvider>. 

这意味着您尚未在main.dart文件中提到提供商类。
为此,您必须使用以下代码。

Provider(
  create: (_) => QuestionProvider(),
  child: MyApp(),
),

如果您有多个提供商,则可以使用以下代码。

MultiProvider(
 providers: [
      QuestionProvider(),
      .....
     ]

  )
 Make sure that Consumer<QuestionProvider> is under your MultiProvider/Provider<QuestionProvider>. 

This means you have not mentioned the provider class in your main.dart file.
For that, you have to use the following code.

Provider(
  create: (_) => QuestionProvider(),
  child: MyApp(),
),

If you have multiple providers you can use the following code.

MultiProvider(
 providers: [
      QuestionProvider(),
      .....
     ]

  )
下雨或天晴 2025-01-27 06:50:57

你为什么使用
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

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