跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Could not find the correct Provider 报错
未分類
11 10 月 2020

Could not find the correct Provider 报错

Could not find the correct Provider 报错

資深大佬 : Kaco 7

使用的是 Provider4.3.2

根据教程在 main 中引入 Provider

void main() {   //强制竖屏   WidgetsFlutterBinding.ensureInitialized();   SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])       .then((_) {     runApp(MultiProvider(       providers: [         ChangeNotifierProvider(create: (context) => Counter()),         ChangeNotifierProvider(create: (context) => CartProvider()),       ],       child: MyApp(),     ));   }); } 

首页是用 PageView 切换四个页面分别是:首页-分类-购物车-我的 在首页-分类 下使用

final counter = Provider.of<Counter>(context); 

会报错 Error: Could not find the correct Provider<Counter> above this CategoryPage Widget

而 购物车-我的 这两个页面则不会,这两个页面是空白的,尝试吧代码复制给首页-分类都依然报错

Widget build(BuildContext context) {     final counter = Provider.of<Counter>(context);     final cart = Provider.of<CartProvider>(context);      return Scaffold(       appBar: AppBar(         elevation: 0, // 隐藏阴影         title: Text("我的"),       ),       body: Center(child: Text("${counter.count}")),     );   } 

大佬有話說 (2)

  • 資深大佬 : kazeik

    加个参数 listen:false 试试

  • 主 資深大佬 : Kaco

    @kazeik 木有用…

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具