riverpod_state 0.1.1
riverpod_state: ^0.1.1 copied to clipboard
The missing idle state to async provider, support code generation
Flutter Riverpod extension adds an extra state for async value (Idle)
Features #
- ✅ new state AsyncIdle
- ✅ support code generation
Getting started #
riverpod_state: ^0.0.1
Usage #
mixin your notifier with AutoDisposeAsyncNotifierMixin
and provide your call in buildAsync method
provider:
@riverpod
class Provider extends _$Provider with AsyncXNotifierMixin<T> {
@override
BuildXCallback<T> build() => idle();
RunXCallback<T> run() => handle(() => ...);
}
ui:
class AddProduct extends ConsumerWidget {
const AddProduct({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final addProduct = ref.watch(addToCartProvider);
return Scaffold(
body: Column(
children: [
if (addProduct.hasIdleValue) const Text("Idle"),
TextButton(
onPressed: () async {
await ref.read(addToCartProvider.notifier).addToCart(1);
ref.read(addToCartProvider).whenDataOrError(data: (value) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Added to cart"),
),
);
}, error: (error, stackTrace) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Error"),
),
);
});
},
child: const Text("Add to cart"),
),
],
),
);
}
}
Additional information #
- updateState is a helper method to update the state of the notifier with data