riverpod_state 0.1.1 copy "riverpod_state: ^0.1.1" to clipboard
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
2
likes
130
points
50
downloads

Publisher

unverified uploader

Weekly Downloads

The missing idle state to async provider, support code generation

Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (license)

Dependencies

flutter, flutter_riverpod, riverpod_annotation

More

Packages that depend on riverpod_state