当前位置: 代码迷 >> 综合 >> Flutter:Could not find the correct Provider
  详细解决方案

Flutter:Could not find the correct Provider

热度:78   发布时间:2024-02-23 21:56:23.0

1 已在页面中进行了定义:

  ShoppingCartCheckOutProvider shoppingCartCheckOutProvider;

2 已在页面中build函数中进行了赋值:

this.shoppingCartCheckOutProvider = Provider.of<ShoppingCartCheckOutProvider>(context);

程序运行时提示这个错误:

I/flutter ( 4223): Another exception was thrown: Error: Could not find the correct Provider<ShoppingCartCheckOutProvider> above this ShoppingCartPage2 Widget

原因:未全局定义声明

解决方法:在main.dart文件中做如下操作声明定义即可:

providers: [

        ....

        ChangeNotifierProvider(builder: (_) => ShoppingCartCheckOutProvider()),

      ],

 

 

 

  相关解决方案