PersistedBloc<S> constructor

PersistedBloc<S>({
  1. S? initialState,
  2. BlocMonitor<Bloc, dynamic> monitor = const BlocEventsPrinter(),
  3. Object tag = 0,
  4. bool autoPersistence = false,
  5. bool recoverStateOnStart = false,
})

Implementation

PersistedBloc({
  S? initialState,
  BlocMonitor monitor = const BlocEventsPrinter(),
  this.tag = 0,
  bool autoPersistence = false,
  bool recoverStateOnStart = false,
})  : _autoPersistence = autoPersistence,
      _recoverStateOnStart = recoverStateOnStart,
      super(
        initialState:
            (autoPersistence && recoverStateOnStart) ? null : initialState,
        monitor: monitor,
      ) {
  if (_autoPersistence && _recoverStateOnStart) {
    _persistenceService.get<S>('value').then((got) {
      if (got == null) {
        if (initialState != null) {
          // if initialState is null then the bloc's is already set to busy
          super.setState(initialState, event: 'initializing');
        }
      } else {
        super.setState(got, event: 'recovered_state');
      }
    });
  }
}