sync<T> static method
void
sync<T>({})
Synchronously executes an operation and handles various types of responses and errors.
The sync method takes the following parameters:
operation: The synchronous operation to execute.onError: A callback to execute when the operation throws an unhandled exception.onNull: A callback to execute when the operation returnsnull.onEmpty: A callback to execute when the operation returns an emptyListorMap.onSuccess: A callback to execute when the operation completes successfully. The callback takes the operation's result as a parameter.
If the operation returns a null or empty response, the corresponding callback functions are
executed. If the operation completes successfully, the onSuccess callback function is
executed. If an error occurs during the execution of the operation, the onError callback
function is executed.
Implementation
static void sync<T>({
/// The synchronous operation to be executed.
required T Function() operation,
/// Callback function to be executed when an error occurs during the execution of the operation.
OnError onError,
/// Callback function to be executed when the operation returns null.
OnNull onNull,
/// Callback function to be executed when the operation returns an empty list or map.
OnEmpty onEmpty,
/// Callback function to be executed when the operation is successful.
void Function(T data)? onSuccess,
}) {
/// Synchronously executes an operation and handles various types of responses and errors.
///
/// This method executes the `operation` argument and waits for the operation to complete. If the operation returns
/// a null or empty response, the corresponding callback functions are executed. If the operation completes successfully,
/// the `onSuccess` callback function is executed.
///
/// If an error occurs during the execution of the operation, the `onError` callback function is executed.
try {
final result = operation.call();
if (result is List) {
onEmpty?.call();
} else if (result is Map) {
onEmpty?.call();
} else if (result == null) {
onNull?.call();
} else {
return onSuccess?.call(result);
}
} catch (e, s) {
onError?.call(e, s);
}
}