redux_saga 3.1.0 copy "redux_saga: ^3.1.0" to clipboard
redux_saga: ^3.1.0 copied to clipboard

redux_saga is a library that aims to make application side effects easier to manage, more efficient to execute, easy to test, and better at handling failures.

example/main.dart

import 'package:redux/redux.dart';
import 'package:redux_saga/redux_saga.dart';

// Reducer method
// Changes state according to the actions dispatched
int counterReducer(int state, dynamic action) {
  if (action is IncrementAction) {
    return state + 1;
  } else if (action is DecrementAction) {
    return state - 1;
  }

  return state;
}

//Actions
class IncrementAction {}

class DecrementAction {}

class IncrementAsyncAction {}

//incrementAsync Saga increasing count delayed
Iterable incrementAsync({dynamic action}) sync* {
  yield Delay(Duration(seconds: 1));
  yield Put(IncrementAction());
}

// counterSaga takes every IncrementAsyncAction
// action and forks incrementAsync
Iterable counterSaga() sync* {
  yield TakeEvery(incrementAsync, pattern: IncrementAsyncAction);
}

void main() {
  // create middleware
  var sagaMiddleware = createSagaMiddleware();

  // create store and apply middleware
  final store = Store<int>(
    counterReducer,
    initialState: 0,
    middleware: [applyMiddleware(sagaMiddleware)],
  );

  // attach store
  sagaMiddleware.setStore(store);

  // run root saga
  sagaMiddleware.run(counterSaga);

  //subscribe to the store
  store.onChange.listen(render);

  //dispatch some sample events
  store.dispatch(IncrementAction());
  store.dispatch(IncrementAction());
  store.dispatch(IncrementAction());

  store.dispatch(DecrementAction());

  store.dispatch(IncrementAsyncAction());

  // Output :
  // 1
  // 2
  // 3
  // 2
  // 2
  // 3
}

//this method may render the ui according to the store data
//now it is just printing to the console for every change
void render(int state) {
  print(state);
}
copied to clipboard
31
likes
150
points
53.9k
downloads

Publisher

unverified uploader

Weekly Downloads

2024.09.13 - 2025.03.28

redux_saga is a library that aims to make application side effects easier to manage, more efficient to execute, easy to test, and better at handling failures.

Repository (GitHub)
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

redux

More

Packages that depend on redux_saga