BaseBloc class abstract
Base bloc class that provides network request management with automatic cancellation.
Features:
- Automatically creates and tracks CancelTokens for all network requests
- Cancels all pending requests when the bloc is disposed/closed
- Prevents memory leaks and unnecessary API calls when user navigates away
Usage:
// 1. Update repository method to accept CancelToken:
Future<DataModel> getData({CancelToken? cancelToken}) async {
var response = await NetworkProvider().request(
Api.endpoint,
method: HTTPMethod.get,
cancelToken: cancelToken, // Pass the token
);
return DataModel.fromJson(response);
}
// 2. Update bloc to pass CancelToken:
await networkRequest(
(CancelToken cancelToken) async => await _repo.getData(cancelToken: cancelToken),
onSuccess: (response) {
// Handle success
},
);
When the bloc is disposed, all pending requests are automatically cancelled.
Constructors
- BaseBloc({BaseStates? initialState})
Properties
- errorController ↔ StreamController
-
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether the bloc is closed.
no setterinherited
- loadingController ↔ StreamController
-
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → BaseStates
-
The current state.
no setterinherited
-
stream
→ Stream<
BaseStates> -
The current stream of states.
no setterinherited
Methods
-
add(
BaseEvent event) → void -
Notifies the
Blocof a neweventwhich triggers all correspondingEventHandlerinstances.inherited -
addError(
Object error, [StackTrace? stackTrace]) → void -
Reports an
errorwhich triggersonErrorwith an optional StackTrace.inherited -
cancelAllRequests(
) → void - Cancels all active requests. This is automatically called when the bloc is disposed/closed.
-
close(
) → Future< void> -
Closes the
eventandstateStreams. This method should be called when aBlocis no longer needed. Once close is called,eventsthat are added will not be processed. In addition, if close is called whileeventsare still being processed, theBlocwill finish processing the pendingevents. -
createCancelToken(
) → CancelToken - Creates a new CancelToken and adds it to the list of active tokens. This token should be passed to repository methods that accept CancelToken.
-
dispose(
) → void -
emit(
BaseStates state) → void -
emitis only for internal use and should never be called directly outside of tests. TheEmitterinstance provided to eachEventHandlershould be used instead.inherited -
handleResponse<
T> (dynamic error, Future< T> request(CancelToken cancelToken), {bool showLoadingBar = true, bool showProgress = false, dynamic onSuccess, FutureOr<void> handleErrorCode(dynamic)?}) → Future<void> -
networkLoadRequest<
T> (Future< T> request(CancelToken cancelToken), {required Future onSuccess(T response), bool showLoadingBar = true, bool showProgress = false, FutureOr<void> handleError(dynamic)?, dynamic errorAction()?, CancelToken? cancelToken}) → Future<T?> -
networkRequest<
T> (Future< T> request(CancelToken cancelToken), {required dynamic onSuccess(T response), bool showLoadingBar = true, bool showProgress = false, FutureOr<void> handleError(dynamic)?, FutureOr<void> handleErrorCode(dynamic)?, dynamic errorAction()?, CancelToken? cancelToken, dynamic handleErrorMessage(T response)?}) → Future<T?> -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on<
E extends Event> (EventHandler< E, BaseStates> 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< BaseStates> change) → void -
Called whenever a
changeoccurs with the givenchange. Achangeoccurs when a newstateis emitted.onChangeis called before thestateof thecubitis updated.onChangeis a great spot to add logging/analytics for a specificcubit.inherited -
onDone(
BaseEvent event, [Object? error, StackTrace? stackTrace]) → void -
Called whenever an
eventhandler for a specificBlochas 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 notifiesBlocObserver.onError.inherited -
onEvent(
BaseEvent event) → void -
Called whenever an
eventisadded to theBloc. A great spot to add logging/analytics at the individualBloclevel.inherited -
onTransition(
Transition< BaseEvent, BaseStates> transition) → void -
Called whenever a
transitionoccurs with the giventransition. Atransitionoccurs when a neweventis added and a new state isemittedfrom a correspondingEventHandler.inherited -
showLoading(
) → void -
startListener(
) → void -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited