Nice and simple app global state based on bloc package


Create a global state for your app (add optional onChangeCallback):

final NeatState<AppState> appState = NeatState<AppState>(
  initialState: const AppState(counter: 0),
  onChangeCallback: (Change<AppState> change) {
    print('exampleBasic:onChangeCallback: ${change.currentState} => ${change.nextState}');

Get access to value of state:

print('exampleBasic: init, counter=${appState.state.counter}');

Change state with replace method:

appState.replace(appState.state.copyWith(counter: appState.state.counter + 1));

or with update method (sometimes it may be more convenient):

appState.update((AppState state) => state.copyWith(counter: state.counter + 1));

Listen to the state changes:

final StreamSubscription<AppState> subscription1 = state) {
  print('exampleBasic:subscription1: counter=${state.counter}');

Listen only to some part of state changes:

final StreamSubscription<int> subscription2 =
      appState.subState<int>((AppState state) => state.counter).listen((int counter) {
    print('exampleBasic:subscription2: counter=$counter');

Additional information

bloc Single source of truth