ErrFlow<T> constructor

ErrFlow<T>([
  1. T? defaultValue
])

Creates an ErrFlow that facilitates handling and logging of errors.

The generic type T is the type of error values. The provided defaultValue is used as the initial value in notifiers, representing the state where there is no error initially in each scope().

Implementation

ErrFlow([T? defaultValue]) {
  _notifier = Notifier<T>(defaultValue)
    ..addListener(
      ({T? error, Object? exception, StackTrace? stack, Object? context}) {
        assert(
          exception == null || logger != null,
          'Information on an exception was provided by `set()` or `log()` '
          'while no logger is set.\n'
          'To fix, set a custom logger by assigning it to `logger`, '
          'or use the default logger by calling `useDefaultLogger()`.',
        );
        assert(
          (stack == null && context == null) || exception != null,
          'Only the stack trace and/or the context, without information '
          'about the exception, were provided by `set()` or `log()`.\n'
          'To fix, provide also the exception.',
        );

        if (logger != null && exception != null) {
          logger!(exception, stack, reason: context);
        }
      },
    );
}