我无法修复:在此 ProductItem 小部件上方找不到正确的 Provider<AppUser>
i can't fix : Could not find the correct Provider<AppUser> above this ProductItem Widget
这是代码:
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
> Whosebug: I/flutter (17444):
> https://whosebug.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 因为我给了值从流提供者到用户模块
我通过将所有代码带到另一个新项目并清理它来解决这个问题,同时升级第三方依赖项
一切都是固定的
这是代码:
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
> Whosebug: I/flutter (17444):
> https://whosebug.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 因为我给了值从流提供者到用户模块
我通过将所有代码带到另一个新项目并清理它来解决这个问题,同时升级第三方依赖项 一切都是固定的