flutter: You used a `BuildContext` that is an ancestor of the provider you are trying to read

本文介绍了在使用ChangeNotifierProvider绑定ViewModel时遇到的'BuildContext'祖先错误。错误日志显示在StatefulWidget中使用提供者时出现问题。解决方法是在布局最外层使用Consumer<ViewModel>(builder: XXX)来包裹内容。
摘要由CSDN通过智能技术生成

当在StatefulWidget中使用ChangeNotifierProvider绑定ViewModel,就会报如上错误,完整的错误日志如下 

 

Error: Could not find the correct Provider<RecordTopViewModel> above this RecordTop Widget

This likely happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:

- 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 RecordTop is under your MultiProvider/Provider<RecordTopViewModel>.
  This usually happen when you are creating a provider and trying to read it immediately.

  For example, instead of:

  ```
  Widget build(BuildContext context) {
    return Provider<Example>(
      create: (_) => Examp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值