【问题标题】:Flutter provider error 'Could not find the correct Provider'Flutter 提供程序错误“找不到正确的提供程序”
【发布时间】:2021-12-15 15:52:09
【问题描述】:

我收到了错误:

The following ProviderNotFoundException was thrown building Widget1(dirty):
Error: Could not find the correct Provider<MyUser> above this FeedView 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 FeedView is under your MultiProvider/Provider<MyUser>.
  This usually happens when you are creating a provider and trying to read it immediately.

在我的颤振应用中使用提供者时。

我在 stackoverflow 上查看了这个问题的答案,但在我的具体情况下找不到任何我做错的事情。

我的代码:

imports...

class FirstPage extends StatefulWidget {
  FirstPage({required this.user});

  final MyUser user;

  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  int selectedIndex = 0;
  final List<Widget> children = [
    Widget1(),
    Widget2(),
    Widget3(),
    Widget4(), 
    Widget5(),
  ];

  @override
  Widget build(BuildContext context) {
    return Provider<MyUser>(
      create: (BuildContext context) {
        return widget.user;
      },
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
        currentIndex: selectedIndex,
        onTap: (index) => setState(() {
          selectedIndex = index;
        }),            
         ...
        ),
        body: children[selectedIndex],
      ),
    );
  }
}

小部件1:

class Widget1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    MyUser user = Provider.of<MyUser>(context);

    return Scaffold(
    ...
    )
   }
 }

我无法理解为什么这是一个问题,因为 Scaffold 最终将 Widget1 作为 body 是 Provider 的子级。

请帮我解决这个问题。提前致谢。

【问题讨论】:

  • 嗨,如果以防万一,在上面的代码中,子列表被 Text() 小部件替换,我假设 MyUser 是某种 Changenotifier 并且 Firstpage 被称为... Scaffold(body: FirstPage(user: MyUser())),代码运行良好。可以贴一下 Widget1 的代码 sn-p 吗?...
  • @ACR 我已经为 Widget1 包含了 sn-p
  • 错误是否可能来自在使用之前创建小部件?如果你把孩子变成一个函数getChildren(),也会发生同样的情况吗?
  • 你能告诉你到底在哪一行得到错误吗?...我尝试了你添加的小部件 sn-p...并将 Provider 更改为 ChangeNotifierProvider 以便更改通知家属...除此之外,我无法重现找不到提供者的错误。
  • 你也可以试试我试过的这个sn-p....我会把它粘贴为答案。

标签: flutter dart provider flutter-provider


【解决方案1】:
Try this snippet if you get some hints. Call it like.....FirstPage(user: MyUser()). 
What this does:
1) In the appbar, there is an action which changes the widget. Tried this to check if the widget renders and selects the proper index.
2) There is a Click button under the widget, which changes the change notifier variable and sees that the widget takes the change and rebuilds.This is just a check.


class MyUser extends ChangeNotifier {
String something = "Something";

_changeAndNotify() {
something = "Nothing";
notifyListeners();
}
}

class FirstPage extends StatefulWidget {
FirstPage({required this.user});

final MyUser user;

@override
_FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
int selectedIndex = 0;
final List<Widget> children = [
Widget1(),
Widget2(),
Text("onething"),
Text("anything"),
];

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyUser>(
  create: (BuildContext context) {
    return widget.user;
  },
  child: Scaffold(
    appBar: AppBar(
      actions: [
        ElevatedButton(
            onPressed: () {
              setState(() {
                selectedIndex = 1;
              });
            },
            child: Text("Change Widget"))
      ],
    ),
    body: Column(
      children: [
        children[selectedIndex],
        ElevatedButton(
            onPressed: () {
              widget.user._changeAndNotify();
            },
            child: Text("Click"))
      ],
    ),
  ),
  );
  }
  }

 class Widget1 extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 MyUser user = Provider.of<MyUser>(context);

 return Text("widget1:${user.something}");
 }
}

class Widget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
MyUser user = Provider.of<MyUser>(context);

return Text("widget2:${user.something}");
}
}

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 2021-11-11
    • 2021-11-19
    • 2020-09-18
    • 1970-01-01
    • 2021-07-22
    • 2021-11-07
    • 2019-12-23
    相关资源
    最近更新 更多