Flutter Riverpod:如何在第一次执行失败后触发 FutureProvider?
Flutter Riverpod: How to trigger FutureProvider after first execution fail?
有没有办法在第一次尝试失败时重试 FutureProvider?
提供商:
final _dataProvider = FutureProvider((ref) => ref.watch(productRepositoryProvider).fetchProducts());
消费者:
return watch(_dataProvider).when(
data: (value) {
return Text('Ok');
},
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) {
return FlatButton(
child: Text('Retry'),
onPressed: () {
// What should I add here to retry this FutureProvider?
},
);
},
);
我认为最适合您尝试做的事情的方法是这样的:
StreamProvider 和 Autodispose
final productRepositoryProvider =
Provider<IProductRepository>((ref) => ProductRepository());
final allProductProvider =
StreamProvider.autoDispose<List<ProductModel>>((ref) {
final _productRepository = ref.watch(productRepositoryProvider);
return _productRepository.allProductsStream;
});
你可以试试
onPressed: () {
context.refresh(_dataProvider);
},
有没有办法在第一次尝试失败时重试 FutureProvider?
提供商:
final _dataProvider = FutureProvider((ref) => ref.watch(productRepositoryProvider).fetchProducts());
消费者:
return watch(_dataProvider).when(
data: (value) {
return Text('Ok');
},
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) {
return FlatButton(
child: Text('Retry'),
onPressed: () {
// What should I add here to retry this FutureProvider?
},
);
},
);
我认为最适合您尝试做的事情的方法是这样的:
StreamProvider 和 Autodispose
final productRepositoryProvider =
Provider<IProductRepository>((ref) => ProductRepository());
final allProductProvider =
StreamProvider.autoDispose<List<ProductModel>>((ref) {
final _productRepository = ref.watch(productRepositoryProvider);
return _productRepository.allProductsStream;
});
你可以试试
onPressed: () {
context.refresh(_dataProvider);
},