addCallbacks method

  1. @useResult
UserException addCallbacks({
  1. VoidCallback? onOk,
  2. VoidCallback? onCancel,
})

Adds callbacks to the UserException.

This method is used to add onOk and onCancel callbacks to the UserException.

The onOk callback will be called when the user taps OK in the error dialog. The onCancel callback will be called when the user taps CANCEL in the error dialog.

If the exception already had callbacks, the new callbacks will be merged with the old ones, and the old callbacks will be called before the new ones.

Implementation

@useResult
UserException addCallbacks({
  VoidCallback? onOk,
  VoidCallback? onCancel,
}) {
  var exception = this;

  if (exception is AdvancedUserException) {
    VoidCallback? _onOk;
    VoidCallback? _onCancel;

    if (exception.onOk == null)
      _onOk = onOk;
    else
      _onOk = () {
        exception.onOk?.call();
        onOk?.call();
      };

    if (exception.onCancel == null)
      _onCancel = onCancel;
    else
      _onCancel = () {
        exception.onCancel?.call();
        onCancel?.call();
      };

    return AdvancedUserException(
      message,
      reason: reason,
      code: code,
      onOk: _onOk,
      onCancel: _onCancel,
      props: exception.props,
      hardCause: exception.hardCause,
      errorText: errorText,
      ifOpenDialog: ifOpenDialog,
    );
  }
  //
  else
    return AdvancedUserException(
      message,
      reason: reason,
      code: code,
      errorText: errorText,
      ifOpenDialog: ifOpenDialog,
      onOk: onOk,
      onCancel: onCancel,
      props: const IMapConst<String, dynamic>({}),
      hardCause: null,
    );
}