onChange method

  1. @override
void onChange(
  1. Change<State> change
)
override

Called whenever a change occurs with the given change. A change occurs when a new state is emitted. onChange is called before the state of the cubit is updated. onChange is a great spot to add logging/analytics for a specific cubit.

Note: super.onChange should always be called first.

@override
void onChange(Change change) {
  // Always call super.onChange with the current change
  super.onChange(change);

  // Custom onChange logic goes here
}

See also:

Implementation

@override
void onChange(Change<State> change) {
  super.onChange(change);
  final storage = HydratedBloc.storage;
  final state = change.nextState;
  try {
    final stateJson = _toJson(state);
    if (stateJson != null) {
      storage.write(storageToken, stateJson).then((_) {}, onError: onError);
    }
  } catch (error, stackTrace) {
    onError(error, stackTrace);
    rethrow;
  }
  _state = state;
}