我无法修复:在此 ProductItem 小部件上方找不到正确的 Provider<AppUser>

Posted

技术标签:

【中文标题】我无法修复:在此 ProductItem 小部件上方找不到正确的 Provider<AppUser>【英文标题】:i can't fix : Could not find the correct Provider<AppUser> above this ProductItem Widget 【发布时间】:2021-10-23 23:17:09 【问题描述】:

这里是代码:

StreamProvider<AppUser?>.value(
      value: AuthenticationService().user,
      builder: (context, snapshot) 
        return MyHomePage();
      

这是在 main.dart 我在哪里添加了一个 streamProvider 存储用户登录本模块后的用户id

   class AppUser 
  final String uid;

  AppUser(required this.uid);
  

之后我尝试在另一个小部件中调用它

    final user = Provider.of<AppUser>(context);

return StreamBuilder<Product>(
    stream: DatabaseProductService(pid: pid).product,
    builder: (context, snapshot) 

      return GridTile(
        footer: GridTileBar(
          backgroundColor: Colors.black54,
          title: Text(
            product.title!,
            textAlign: TextAlign.center,
          ),
          leading: IconButton(
            icon: Icon(
              product.favorite! ? Icons.favorite : Icons.favorite_border,
            ),
            onPressed: () async 
              await DatabaseProductService(pid: pid)
                  .productFavoriteState(!product.favorite!);
            ,
          ),
          trailing: IconButton(
            icon: Icon(Icons.add_shopping_cart),
            onPressed: () async 
              await cart.addItem(product, user);
              ScaffoldMessenger.of(context).hideCurrentSnackBar();
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text('Added item to cart'),
                  duration: Duration(seconds: 2),
                  action: SnackBarAction(
                    label: 'UNDO',
                    onPressed: () async 
                      await cart.removeProduct(product.productID!, user);
                    ,
                  ),
                ),
              );
            ,
          ),
        ),
      );
    );

它显示了这个错误

> > flutter (17444): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter
> (17444): The following ProviderNotFoundException was thrown building
> ProductItem(dirty): I/flutter (17444): Error: Could not find the
> correct Provider<AppUser> above this ProductItem Widget I/flutter
> (17444):  I/flutter (17444): This happens because you used a
> `BuildContext` that does not include the provider I/flutter (17444):
> of your choice. There are a few common scenarios: I/flutter (17444): 
> I/flutter (17444): - You added a new provider in your `main.dart` and
> performed a hot-reload. I/flutter (17444):   To fix, perform a
> hot-restart. I/flutter (17444):  I/flutter (17444): - The provider you
> are trying to read is in a different route. I/flutter (17444): 
> I/flutter (17444):   Providers are "scoped". So if you insert of
> provider inside a route, then I/flutter (17444):   other routes will
> not be able to access that provider. I/flutter (17444):  I/flutter
> (17444): - You used a `BuildContext` that is an ancestor of the
> provider you are trying to read. I/flutter (17444):  I/flutter
> (17444):   Make sure that ProductItem is under your
> MultiProvider/Provider<AppUser>. I/flutter (17444):   This usually
> happens when you are creating a provider and trying to read it
> immediately. I/flutter (17444):  I/flutter (17444):   For example,
> instead of: I/flutter (17444):  I/flutter (17444):   ``` I/flutter
> (17444):   Widget build(BuildContext context)  I/flutter (17444):    
> return Provider<Example>( I/flutter (17444):       create: (_) =>
> Example(), I/flutter (17444):       // Will throw a
> ProviderNotFoundError, because `context` is associated I/flutter
> (17444):       // to the widget that is the parent of
> `Provider<Example>` I/flutter (17444):       child:
> Text(context.watch<Example>()), I/flutter (17444):     ), I/flutter
> (17444):    I/flutter (17444):   ``` I/flutter (17444):  I/flutter
> (17444):   consider using `builder` like so: I/flutter (17444): 
> I/flutter (17444):   ``` I/flutter (17444):   Widget
> build(BuildContext context)  I/flutter (17444):     return
> Provider<Example>( I/flutter (17444):       create: (_) => Example(),
> I/flutter (17444):       // we use `builder` to obtain a new
> `BuildContext` that has access to the provider I/flutter (17444):     
> builder: (context)  I/flutter (17444):         // No longer throws
> I/flutter (17444):         return Text(context.watch<Example>()),
> I/flutter (17444):        I/flutter (17444):     ), I/flutter
> (17444):    I/flutter (17444):   ``` I/flutter (17444):  I/flutter
> (17444): If none of these solutions work, consider asking for help on
> ***: I/flutter (17444):
> https://***.com/questions/tagged/flutter I/flutter (17444): 
> I/flutter (17444): The relevant error-causing widget was: I/flutter
> (17444):   ProductItem I/flutter (17444):  
> file:///C:/Users/lenovo/Desktop/first%20vs%20code%20project/shop_App/shop_app/lib/widgets/products_grid.dart:68:22

我试图让用户在函数中从提供者调用我是否需要它总是向我显示相同的问题,即程序找不到正确的提供者我无法尝试使用 cunsumer,因为我将值赋予来自流提供者的用户模块

【问题讨论】:

【参考方案1】:

我通过将所有代码带到另一个新项目并清理它并升级第三方依赖项来解决此问题 一切都解决了

【讨论】:

以上是关于我无法修复:在此 ProductItem 小部件上方找不到正确的 Provider<AppUser>的主要内容,如果未能解决你的问题,请参考以下文章

错误:在此 Directioner 小部件上方找不到正确的 Provider<User>

找不到正确的提供者

css [亚马逊支付]修复移动设备上拉伸或扭曲的亚马逊小部件。

错误:在此小部件上方找不到正确的提供程序 <List<Auftrag>>

如何修复非空字符串必须提供给文本小部件?

如何将旧的小部件迁移到新的小部件