setStateNullable method

FutureOr<T?> setStateNullable(
  1. Object? mutator(
    1. T? s
    ), {
  2. required void middleSetState(
    1. StateStatus,
    2. dynamic result
    ),
  3. required StackTrace? stackTrace,
})

Implementation

FutureOr<T?> setStateNullable(
  Object? Function(T? s) mutator, {
  required void Function(StateStatus, dynamic result) middleSetState,
  required StackTrace? stackTrace,
}) {
  try {
    var result = mutator(_snapState.data);

    if (result is Future || result is Stream) {
      _handleAsyncState(
        mutator: mutator,
        asyncResult: result!,
        middleSetState: middleSetState,
        stackTrace: stackTrace,
      );
      return () async {
        try {
          await stateAsync;
        } catch (e) {
          return _snapState.data;
        }
      }();
    }
    if (_snapState.isWaiting &&
        snapState._infoMessage != kDependsOn &&
        _snapState._infoMessage != kStopWaiting) {
      _snapState = _snapState.copyToHasData(result).copyToIsWaiting();
      notify(shouldOverrideDefaultSideEffects: (_) => true);
    } else {
      middleSetState(
        StateStatus.hasData,
        result,
      );
    }
    return SynchronousFuture(_snapState.data);
  } catch (e, s) {
    middleSetState(
      StateStatus.hasError,
      SnapError(
        error: e is Error && e is! UnimplementedError ? '$e\n$s' : e,
        stackTrace: stackTrace ?? s,
        refresher: () => setStateNullable(
          mutator,
          middleSetState: middleSetState,
          stackTrace: stackTrace,
        ),
      ),
    );
    if (e is Error && e is! UnimplementedError) {
      if (e is TypeError) {
        StatesRebuilerLogger.log('', e);
        StatesRebuilerLogger.log(
          '',
          'IF YOU ARE TESTING THE APP, IT MAY BE THAT THE CALLED METHOD IS NOT MOCKED',
          s,
        );
      } else {
        StatesRebuilerLogger.log('', e, s);
      }

      rethrow;
    }

    return SynchronousFuture(_snapState.data);
  }
}