onCompleteNotNull method

Future<T?> onCompleteNotNull({
  1. required FutureOr<void> onSuccess(
    1. T r
    ),
  2. required FutureOr<void> onErrorOrNull(
    1. Object? e,
    2. StackTrace? s
    ),
  3. AsyncExtensionErrorLogger? errorLogger,
  4. bool logError = true,
})

Calls onSuccess or onErrorOrNull when this Future completes. Calls onErrorOrNull If it completes with null or with an error.

Logs the error using errorLogger or defaultAsyncExtensionErrorLogger if parameter logError is true.

Implementation

Future<T?> onCompleteNotNull({
  required FutureOr<void> Function(T r) onSuccess,
  required FutureOr<void> Function(Object? e, StackTrace? s) onErrorOrNull,
  AsyncExtensionErrorLogger? errorLogger,
  bool logError = true,
}) async {
  try {
    var r = await this;

    if (r == null) {
      await onErrorOrNull(null, null);
    } else {
      await onSuccess(r);
    }

    return r;
  } catch (e, s) {
    if (logError) errorLogger.logError(e, s);

    await onErrorOrNull(e, s);

    return null;
  }
}