cubitCatch method
Executes a given asynchronous action within a try-catch block, handling loading states.
This method provides a convenient way to perform asynchronous operations while automatically managing loading states and error handling.
Parameters:
actions: A required function that contains the asynchronous operations to be performed.isLoading: An optional boolean that determines whether to show/hide loading indicators. Defaults to true.
Usage:
await cubitCatch(
actions: () async {
// Perform some asynchronous operations
await fetchData();
await processData();
},
isLoading: true, // Optional, defaults to true
);
The method will:
- Show a loading indicator if
isLoadingis true. - Execute the provided
actions. - Hide the loading indicator after
actionscomplete or if an error occurs. - Log any errors that occur during execution.
Note: This method uses showLoading and hideLoading internally, which should be implemented in the concrete Bloc class or a mixin.
Implementation
Future<void> cubitCatch({
required Future<void> Function() actions,
bool isLoading = true,
String keyLoading = LoadingKey.global,
Future<void> Function(Object error, StackTrace stackTrace)? onError,
Future<void> Function()? onFinally,
}) async {
await catchError(
actions: actions,
isLoading: isLoading,
keyLoading: keyLoading,
onError: onError,
onFinally: onFinally,
);
}