BaseCubit<T> class
abstract
A Cubit for screens that load data of type T and run actions.
- load fetches the screen's data: emits DataLoading, then DataSuccess or DataFailed. Concurrent loads are safe: only the latest one's result is emitted.
- runAction runs a one-shot action (submit, delete, like): emits ActionLoading, then ActionSuccess or ActionFailed. Errors never escape as unhandled exceptions.
- The
emit*helpers and updateData let you manage the state yourself. - observe gives a section of the screen its own independent state.
Every error from load and runAction goes through onError, so
override it (calling super) to log or report errors.
Nothing is emitted after close, so late responses are harmless.
Constructors
-
BaseCubit([DataState<
T> ? initialState]) -
Creates the cubit, starting at
initialState(default DataInitial).
Properties
-
buildState
→ BuildState<
T> -
The last state the screen rendered.
no setter
- cache → SoftCache
-
In-memory cache used by load and observe.
final
- data → T?
-
The data on screen, or
nullif not loaded.no setter - 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
→ DataState<
T> -
The current state.
no setterinherited
-
stream
→ Stream<
DataState< T> > -
The current stream of states.
no setterinherited
Methods
-
addError(
Object error, [StackTrace? stackTrace]) → void -
Reports an
errorwhich triggersonErrorwith an optional StackTrace.inherited -
close(
) → Future< void> - Closes the instance. This method should be called when the instance is no longer needed. Once close is called, the instance can no longer be used.
-
emit(
DataState< T> state) → void -
Updates the
stateto the providedstate.emitdoes nothing if thestatebeing emitted is equal to the currentstate.inherited -
emitData(
T data) → void -
Shows
data. -
emitError(
Object error, [StackTrace? stackTrace]) → void -
Shows
error. -
emitLoading(
) → void - Shows DataLoading.
-
emitState(
DataState< T> state) → void -
Emits any state, including your own BuildState/ActionState
subclasses. Cancels any load in flight when
stateis a BuildState, so a late response does not overwrite it. -
load(
Future< T> request(), {String? cacheKey, Duration? cacheTtl, bool forceRefresh = false, bool keepDataWhileLoading = false}) → Future<void> -
Loads the screen's data with
request. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
observe<
R> (String tag, Future< R> request(), {String? cacheKey, Duration? cacheTtl, bool forceRefresh = false}) → DataObserver<R> -
Loads
requestinto the observer fortagand returns the observer. -
observer<
R> (String tag) → DataObserver< R> -
Returns the observer for
tag, creating it on first use. -
onChange(
Change< DataState< change) → voidT> > -
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 -
onError(
Object error, StackTrace stackTrace) → void -
Called whenever an
erroroccurs and notifiesBlocObserver.onError.inherited -
runAction<
R> (Future< R> action(), {void onSuccess(R result)?}) → Future<ActionResult< R> > -
Runs a one-shot
actionand returns its ActionResult. -
toString(
) → String -
A string representation of this object.
inherited
-
updateData(
T update(T current)) → void - Changes the data on screen without a request, e.g. to append an item or flip a flag after an action. Does nothing if no data is loaded.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited