(Flutter/Riverpod)StateProvider文档的例子没有更新
(Flutter/Riverpod) the example of StateProvider document was not updated
Riverod的StateProvider示例暂未更新
https://pub.dev/documentation/riverpod/latest/riverpod/StateProvider-class.html
final selectedProductIdProvider = StateProvider<String?>((ref) => null);
final productsProvider = StateNotifierProvider<ProductsNotifier, List<Product>>(
(ref) => ProductsNotifier());
Widget build(BuildContext context, WidgetRef ref) {
final List<Product> products = ref.watch(productsProvider);
final selectedProductId = ref.watch(selectedProductIdProvider);
return ListView(
children: [
for (final product in products)
GestureDetector(
onTap: () => selectedProductId.state = product.id,
child: ProductItem(
product: product,
isSelected: selectedProductId.state == product.id,
),
),
],
);
}
现在 Riverpod 已经成为 v1 稳定版,上面的代码不再有效。
以下是 Riverpod v1 稳定版的正确代码吗?
final selectedProductIdProvider = StateProvider<String?>((ref) => null);
final productsProvider = StateNotifierProvider<ProductsNotifier, List<Product>>(
(ref) => ProductsNotifier());
class Foo2 extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final List<Product> products = ref.watch(productsProvider);
final String? selectedProductId = ref.watch(selectedProductIdProvider);
final StateController<String?> selectedProductIdController =
ref.watch(selectedProductIdProvider.notifier);
return ListView(
children: [
for (final product in products)
GestureDetector(
onTap: () => selectedProductIdController.state = product.id,
child: ProductItem(
product: product,
isSelected: selectedProductId == product.id,
),
),
],
);
}
}
糟糕!
您的代码是正确的,尽管您不必在 build
内获取通知程序并且可以在 onTap
:
内获取
onTap: () => ref.read(selectedProductIdProvider.notifier).state = product.id,
Riverod的StateProvider示例暂未更新
https://pub.dev/documentation/riverpod/latest/riverpod/StateProvider-class.html
final selectedProductIdProvider = StateProvider<String?>((ref) => null);
final productsProvider = StateNotifierProvider<ProductsNotifier, List<Product>>(
(ref) => ProductsNotifier());
Widget build(BuildContext context, WidgetRef ref) {
final List<Product> products = ref.watch(productsProvider);
final selectedProductId = ref.watch(selectedProductIdProvider);
return ListView(
children: [
for (final product in products)
GestureDetector(
onTap: () => selectedProductId.state = product.id,
child: ProductItem(
product: product,
isSelected: selectedProductId.state == product.id,
),
),
],
);
}
现在 Riverpod 已经成为 v1 稳定版,上面的代码不再有效。
以下是 Riverpod v1 稳定版的正确代码吗?
final selectedProductIdProvider = StateProvider<String?>((ref) => null);
final productsProvider = StateNotifierProvider<ProductsNotifier, List<Product>>(
(ref) => ProductsNotifier());
class Foo2 extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final List<Product> products = ref.watch(productsProvider);
final String? selectedProductId = ref.watch(selectedProductIdProvider);
final StateController<String?> selectedProductIdController =
ref.watch(selectedProductIdProvider.notifier);
return ListView(
children: [
for (final product in products)
GestureDetector(
onTap: () => selectedProductIdController.state = product.id,
child: ProductItem(
product: product,
isSelected: selectedProductId == product.id,
),
),
],
);
}
}
糟糕!
您的代码是正确的,尽管您不必在 build
内获取通知程序并且可以在 onTap
:
onTap: () => ref.read(selectedProductIdProvider.notifier).state = product.id,