繁体   English   中英

Flutter 提供程序错误“找不到正确的提供程序”

[英]Flutter provider error 'Could not find the correct Provider'

我收到错误:

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.

在我的 flutter 应用程序中使用 provider 时。

我在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 的孩子。

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

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}");
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM