BaseBloc class abstract
BaseBloc serves as the foundational class for managing state transitions in a reactive application architecture using the BLoC pattern.
This abstract class extends Bloc from the flutter_bloc package and
provides common utilities for handling API responses, managing various
error states, and enabling easy navigation during specific scenarios.
Key Features:
-
Centralized Handling of API Responses: The handleApiResponse method processes API responses and transitions to appropriate states based on the type of response:
- Success: Managed using stateFactory or a default state.
- API Errors: Handled by handleApiError and handleErrorAndException.
- Network Errors: Managed using handleNetworkException.
- Parsing Errors: Also processed by handleNetworkException.
- No Internet Connection: Redirected using handleNoInternetException.
- Unauthorized Errors: Managed with handleUnAuthorizedException.
-
Simplified State Management: Through the BaseFactory, states are dynamically created, reducing repetitive state initialization logic.
-
Error Classification: Helper methods such as returnErrorHandler, handleApiError, and handleErrorAndException provide detailed classification and processing for different error types, ensuring consistency across the application.
-
Navigation Utility: The
_navigatemethod supports navigation to error-specific screens such as:- NoInternetScreen: Displayed during no internet scenarios via handleNoInternetException.
- MaintenanceScreen: Triggered during server maintenance through handleServerErrorException.
-
Lifecycle Management: Implements onDispose and overrides close to ensure resources are properly released when the BLoC is disposed.
Constructors
- BaseBloc(BaseFactory? stateFactory, {BaseState? initialState})
- Constructor for initializing the BaseBloc.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether the bloc is closed.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → BaseState
-
The current state.
no setterinherited
- stateFactory → BaseFactory?
-
Factory for creating specific states dynamically based on the API response.
final
-
stream
→ Stream<
BaseState> -
The current stream of states.
no setterinherited
Methods
-
add(
BaseEvent event) → void -
Notifies the Bloc of a new
eventwhich triggers all corresponding EventHandler instances.inherited -
addError(
Object error, [StackTrace? stackTrace]) → void -
Reports an
errorwhich triggers onError with an optional StackTrace.inherited -
close(
) → Future< void> -
Closes the
eventandstateStreams. This method should be called when a Bloc is no longer needed. Once close is called,eventsthat are added will not be processed. In addition, if close is called whileeventsare still being processed, the Bloc will finish processing the pendingevents.override -
emit(
BaseState state) → void -
emit is only for internal use and should never be called directly
outside of tests. The Emitter instance provided to each EventHandler
should be used instead.
inherited
-
handleApiError<
M> (ApiResponse< M> ? apiResponse, {BaseState? onApiErrorAction(int?, String?)?, String? errorType}) → dynamic - Handles API errors, such as unauthorized access or server-side validation failures.
-
handleApiResponse<
M> (ApiResponse< M> ? apiResponse, {BaseState? onApiErrorAction(int?, String?)?, String? errorType, required Function? retryFunc}) → dynamic - Handles API responses and determines the next state based on the response type.
-
handleErrorAndException<
M> (ApiResponse< M> ? apiResponse, {BaseState? onApiErrorAction(int?, String?)?, String? errorType}) → dynamic - Handles API and network-related errors, including unauthorized access.
-
handleNetworkException(
ApiResponse? apiResponse, {String? errorType}) → dynamic - Handles network-related errors, such as connectivity issues or request failures.
-
handleNoInternetException(
ApiResponse? apiResponse, {String? errorType, Function? retryFunc}) → dynamic - Handles no internet connection errors.
-
handleServerErrorException(
ApiResponse? apiResponse, {String? errorType, Function? retryFunc}) → dynamic - Handles server errors, such as maintenance or server-side issues.
-
handleUnAuthorizedException(
ApiResponse? apiResponse, {String? errorType, Function? retryFunc}) → dynamic - Handles unauthorized access errors.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on<
E extends Event> (EventHandler< E, BaseState> handler, {EventTransformer<E> ? transformer}) → void -
Register event handler for an event of type
E. There should only ever be one event handler per event typeE.inherited -
onChange(
Change< BaseState> change) → void -
Called whenever a
changeoccurs with the givenchange. Achangeoccurs when a newstateis emitted. onChange is called before thestateof thecubitis updated. onChange is a great spot to add logging/analytics for a specificcubit.inherited -
onDispose(
) → void - A lifecycle method to clean up resources when the BaseBloc is disposed.
-
onDone(
BaseEvent event, [Object? error, StackTrace? stackTrace]) → void -
Called whenever an
eventhandler for a specific Bloc has completed. This may include anerrorandstackTraceif an uncaught exception occurred within the event handler.inherited -
onError(
Object error, StackTrace stackTrace) → void -
Called whenever an
erroroccurs and notifies BlocObserver.onError.inherited -
onEvent(
BaseEvent event) → void -
Called whenever an
eventis added to the Bloc. A great spot to add logging/analytics at the individual Bloc level.inherited -
onTransition(
Transition< BaseEvent, BaseState> transition) → void -
Called whenever a
transitionoccurs with the giventransition. Atransitionoccurs when a neweventis added and a new state isemittedfrom a corresponding EventHandler.inherited -
returnErrorHandler(
{String? errorMsg, bool isException = false, String? errorType, ApiResponseType? apiErrorType}) → dynamic - Returns an error state based on the error type and message.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited