reflux library

Reflux - Redux-inspired state management for React with Dart and Flutter.

Reflux provides a unidirectional data flow architecture with full type safety using Dart's type system and sealed classes for exhaustive pattern matching. Share your state logic across React web apps and Flutter mobile.

Core Concepts

  • Store: Holds the complete state tree of your application.
  • Actions: Sealed classes that describe what happened - pattern match on the actual TYPE, not strings!
  • Reducers: Pure functions that specify how state changes in response to actions.
  • Middleware: Extension points for adding custom behavior.
  • Selectors: Functions that extract and memoize derived data.

Example

// Define state as a record
typedef CounterState = ({int count});

// Define actions as sealed classes sealed class CounterAction extends Action {} final class Increment extends CounterAction {} final class Decrement extends CounterAction {} final class SetValue extends CounterAction { const SetValue(this.value); final int value; }

// Create a reducer with exhaustive pattern matching CounterState counterReducer(CounterState state, Action action) => switch (action) { Increment() => (count: state.count + 1), Decrement() => (count: state.count - 1), SetValue(:final value) => (count: value), _ => state, };

// Create the store final store = createStore(counterReducer, (count: 0));

// Subscribe to changes store.subscribe(() => print('Count: ${store.getState().count}'));

// Dispatch actions store.dispatch(Increment()); store.dispatch(Decrement()); store.dispatch(SetValue(42));

Classes

Action

Base class for all actions. Extend this to create type-safe actions with pattern matching.

InitAction

System action for store initialization.

ReplaceAction

System action for reducer replacement.

ResettableSelector<S, R>

A resettable memoized selector.

Store<S>

Store interface that defines the contract for a state container.

TimeTravelAction

System action for time travel.

TimeTravelEnhancer<S>

Time travel enhancer that allows rewinding and replaying actions.

Constants

initAction → const Action

The init action dispatched when the store is created.

Functions

applyMiddleware<S>(List<Middleware<S>> middlewares) StoreEnhancer<S>

Applies middleware to the dispatch method of the store.

bindActionCreator<A extends Action>(A actionCreator(), void dispatch(Action)) → void Function()

Binds a single action creator to a dispatch function.

bindActionCreators(Map<String, Action Function()> actionCreators, void dispatch(Action)) Map<String, void Function()>

Binds multiple action creators to a dispatch function.

combineReducers<S>({required S initialState, required List<SliceDefinition<S, dynamic>> slices}) Reducer<S>

Combines multiple reducers into a single reducer function.

combineReducersMap(Map<String, Reducer<Object?>> reducers) Reducer<Map<String, Object?>>

A simpler way to combine reducers using a Map-based state.

compose<T>(List<T Function(T)> functions) → T Function(T)

Composes single-argument functions from right to left.

composeEnhancers<S>(List<StoreEnhancer<S>> enhancers) StoreEnhancer<S>

Composes multiple store enhancers into a single enhancer.

createDispatcher<A extends Action>(A actionCreator(), void dispatch(Action)) → void Function()

Creates a dispatcher function for a specific action type.

createDispatcherWith<A extends Action, P>(A actionCreator(P), void dispatch(Action)) → void Function(P)

Creates a dispatcher function for an action with a parameter.

createEnhancer<S>(Store<S> enhance(Store<S> store)) StoreEnhancer<S>

Creates an enhancer that wraps the store with additional functionality.

createSelector1<S, T1, R>(Selector<S, T1> selector1, R combiner(T1)) Selector<S, R>

Creates a memoized selector that caches its result.

createSelector2<S, T1, T2, R>(Selector<S, T1> selector1, Selector<S, T2> selector2, R combiner(T1, T2)) Selector<S, R>

Creates a memoized selector with two input selectors.

createSelector3<S, T1, T2, T3, R>(Selector<S, T1> selector1, Selector<S, T2> selector2, Selector<S, T3> selector3, R combiner(T1, T2, T3)) Selector<S, R>

Creates a memoized selector with three input selectors.

createSelector4<S, T1, T2, T3, T4, R>(Selector<S, T1> selector1, Selector<S, T2> selector2, Selector<S, T3> selector3, Selector<S, T4> selector4, R combiner(T1, T2, T3, T4)) Selector<S, R>

Creates a memoized selector with four input selectors.

createSelector5<S, T1, T2, T3, T4, T5, R>(Selector<S, T1> selector1, Selector<S, T2> selector2, Selector<S, T3> selector3, Selector<S, T4> selector4, Selector<S, T5> selector5, R combiner(T1, T2, T3, T4, T5)) Selector<S, R>

Creates a memoized selector with five input selectors.

createStore<S>(Reducer<S> reducer, S preloadedState, {StoreEnhancer<S>? enhancer}) Store<S>

Creates a store that holds the state tree.

createStructuredSelector<S, R>(R compute(S state)) Selector<S, R>

Structured selector that computes multiple derived values at once.

devToolsEnhancer<S>({String name = 'Store', void onAction(Action action, S prevState, S nextState)?, bool enabled = true}) StoreEnhancer<S>

Dev tools enhancer that logs all actions and state changes.

pipe2<A, B, C>(B f1(A), C f2(B)) → C Function(A)

Composes functions with different input/output types.

pipe3<A, B, C, D>(B f1(A), C f2(B), D f3(C)) → D Function(A)

Three-function pipeline composition.

pipe4<A, B, C, D, E>(B f1(A), C f2(B), D f3(C), E f4(D)) → E Function(A)

Four-function pipeline composition.

pipe5<A, B, C, D, E, F>(B f1(A), C f2(B), D f3(C), E f4(D), F f5(E)) → F Function(A)

Five-function pipeline composition.

Typedefs

ActionCreator<A extends Action> = A Function()
An action creator is a function that creates an action.
Middleware<S> = MiddlewareTransform Function(MiddlewareApi<S> api)
Middleware is a higher-order function that composes a dispatch function to return a new dispatch function. It provides a third-party extension point between dispatching an action and the moment it reaches the reducer.
MiddlewareApi<S> = ({void Function(Action action) dispatch, S Function() getState})
The API available to middleware.
MiddlewareTransform = NextDispatcher Function(NextDispatcher next)
A middleware transform takes the next dispatcher and returns a new one.
NextDispatcher = void Function(Action action)
The next dispatcher in the middleware chain.
Reducer<S> = S Function(S state, Action action)
A reducer specifies how the application's state changes in response to actions sent to the store.
Selector<S, R> = R Function(S state)
A basic selector that extracts data from state.
SliceDefinition<S, K> = ({K initialState, SliceReducer<S, K> reducer, K Function(S) selector, S Function(S, K) updater})
Definition for a state slice with its key, reducer, and initial state.
SliceReducer<S, K> = K Function(K state, Action action)
A slice reducer handles a specific part of the state tree.
StateListener<S> = void Function()
A listener that is called when the state changes.
StoreCreator<S> = Store<S> Function(Reducer<S> reducer, S preloadedState)
A store creator function.
StoreEnhancer<S> = Store<S> Function(StoreCreator<S> createStore, Reducer<S> reducer, S preloadedState)
A store enhancer is a higher-order function that composes a store creator to return a new enhanced store creator.
Unsubscribe = void Function()
Unsubscribe function returned by subscribe.

Exceptions / Errors

DispatchInReducerException

Exception thrown when an action is dispatched while a reducer is executing.

SubscribeInReducerException

Exception thrown when subscribe is called from within a reducer.