sinew_riverpod

pub package License Docs

Riverpod notifier mixins, hooks and AsyncValue conversions for Sinew's presentation pattern.

The presentation pattern of sinew_presentation, as mixins on a Riverpod Notifier. The emitter lives as long as the notifier, so a rebuild keeps it and the page keeps receiving effects.

Features

  • StateEffectNotifier and EventActionNotifier: setState, emitEffect, onEvent, and the pure mapEventToAction split
  • useOnOpened and useEffects: Flutter Hooks that send "the page opened" once, and listen to effects while the page is mounted
  • ViewStateToAsync and AsyncToViewState: convert between ViewState and Riverpod's AsyncValue

Getting started

flutter pub add sinew_riverpod

Usage

A list page in a notifier (from the example app):

class OrderList extends Notifier<OrderListState>
    with
        StateEffectNotifier<OrderListState, Never>,
        EventActionNotifier<OrderListState, OrderListEvent, LoadOrders, Never> {
  late final Pager<Order> _pager = Pager.offset(
    (page, _) => ref.read(orderBackendProvider).orders(page),
    itemKey: (order) => order.id,
  );

  @override
  OrderListState build() => const OrderListState();

  @override
  LoadOrders? mapEventToAction(OrderListEvent event) => switch (event) {
    OrderListOpened() => const LoadOrders(LoadType.initial),
    OrderListEndReached() =>
      state.orders.canLoadMore ? const LoadOrders(LoadType.more) : null,
    OrderListRetryTapped() => LoadOrders(state.orders.retryType),
    OrderListRefreshed() => const LoadOrders(LoadType.refresh),
  };

  @override
  Future<void> onAction(LoadOrders action) => _pager.load(
    action.type,
    state.orders,
    update: (next) => setState((_) => OrderListState(orders: next)),
  );
}

The page calls ref.read(orderListProvider.notifier).onEvent(...).

Sinew is one architecture for Flutter and Kotlin, released as a set of packages at one version.

Documentation

License

Apache 2.0, see LICENSE.

Libraries

sinew_riverpod
Riverpod notifier mixins, hooks and AsyncValue conversions for Sinew's presentation pattern.