无法访问供应商

Not able to access the provider in flutter

我有一个简单的供应商class:-

class DataProvider with ChangeNotifier {
   int count;
   void updateCount() {
     count = count + 1;
     notifyListeners();
   }
}

我将此提供商附加到以下 class:-

class MyWidget extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => DataProvider(),
      child: Scaffold(
        body: raisedButton(
          onPressed: () {
            Provider.of<DataProvider>(context).updateCount();
          }
           child: Text("Click!")
         ),
       ),
     ),
   }

及其给我以下错误:-

 I/flutter (32011): Error: Could not find the correct Provider<DataProvider> above this MyWidget 
 Widget
 I/flutter (32011): To fix, please:
 I/flutter (32011):   * Ensure the Provider<DataProvider> is an ancestor to this MyWidget Widget
 I/flutter (32011):   * Provide types to Provider<DataProvider>
 I/flutter (32011):   * Provide types to Consumer<DataProvider>
 I/flutter (32011):   * Provide types to Provider.of<DataProvider>()
 I/flutter (32011):   * Ensure the correct `context` is being used. 

这可能是什么原因?

编辑:- 当我从我定义的小部件的任何子小部件访问提供程序时,它工作正常。 ChangeNotifierProvider.

您需要将 raisedButton 包裹在 Consumer 中。

The Consumer widget has two main purposes:

It allows obtaining a value from a provider when we don't have a BuildContext that is a descendant of said provider, and therefore cannot use Provider.of. This scenario typically happens when the widget that creates the provider is also one of its consumers, like in the following example:

@override
Widget build(BuildContext context) {
  return ChangeNotifierProvider(
    create: (_) => Foo(),
    child: Text(Provider.of<Foo>(context).value),
  );
}

This example will throw a ProviderNotFoundException, because Provider.of is called with a BuildContext that is an ancestor of the provider.

Instead, we can use the Consumer widget, that will call Provider.of with its own BuildContext.

Using Consumer, the previous example will become:

@override
Widget build(BuildContext context) {
  return ChangeNotifierProvider(
    create: (_) => Foo(),
    child: Consumer<Foo>(
      builder: (_, foo, __) => Text(foo.value),
    },
  );
}

This won't throw a ProviderNotFoundException and will correctly build the Text. It will also update the Text whenever the value foo changes.

参见:https://pub.dev/documentation/provider/latest/provider/Consumer-class.html

这是因为您在调用 Provider.of<DataProvider>(context).updateCount() 时使用了错误的 context。您正在使用 build 方法提供的 context,它比您尝试访问的 Provider 更高层次。您需要使用属于 Provider 的后代(较低层级)的 context

将您的 Scaffold 包装在 Builder 小部件中,这会在小部件层次结构的该级别公开一个新的 context,并改用该上下文。

您也可以使用 Consumer,尽管在您的示例中它不是最佳选择,因为每次在 Provider 中调用 notifyListeners()Consumer 都会重建,这将是多余的因为你的 UI 没有改变。

在 Builder 小部件中使用 Scaffold 帮助我解决了 Provider 的问题

Widget build(BuildContext context) {
    return ChangeNotifierProvider<StatusViewModel>(
      create: (context) => StatusViewModel(),
  child: Builder(
    builder: (context){
      return Scaffold(
        appBar: AppBar(
          title: Text("this is appbar"),    
        ),
      );
    },
  ),
);}