UiState<T> mixin
Mixin that provides UI state mapping functionality for network responses.
This mixin adds the ability to convert NetworkState objects into ICubitState objects that can be used by the UI layer. It provides a standardized way to handle network responses and map them to appropriate UI states.
Features
- State Mapping: Converts network states to UI states
- Error Handling: Standardized error message formatting
- Type Safety: Generic type parameter ensures type safety
- Reusable Logic: Can be mixed into any cubit that extends ICubit
Usage Example
class PaymentCubit extends ICubit<PaymentState> with UiState<PaymentState> {
Future<void> processPayment(double amount) async {
final networkState = await _paymentService.processPayment(amount);
final uiState = mapNetworkState(networkState);
emit(uiState);
}
}
Generic Type Parameter
- T: The type of data that the network state contains
- Must match the type parameter of the cubit this mixin is applied to
State Mapping
The mixin maps network states to UI states as follows:
- Initial: Maps to
ICubitState.initial()
- Success: Maps to
ICubitState.success()
with the data - Error: Maps to
ICubitState.error()
with error message and list
- Superclass constraints
- ICubit<
T>
- ICubit<
- Mixin applications
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
→ ICubitState<
T> -
The current state.
no setterinherited
-
stream
→ Stream<
ICubitState< T> > -
The current stream of states.
no setterinherited
Methods
-
addError(
Object error, [StackTrace? stackTrace]) → void -
Reports an
error
which triggersonError
with 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.inherited -
emit(
ICubitState< T> state) → void -
Updates the
state
to the providedstate
.emit
does nothing if thestate
being emitted is equal to the currentstate
.inherited -
mapNetworkState(
NetworkState< T> networkState) → ICubitState<T> - Maps a NetworkState to an ICubitState for UI consumption.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onChange(
Change< ICubitState< change) → voidT> > -
Handles automatic UI updates based on state changes.
inherited
-
onError(
Object error, StackTrace stackTrace) → void -
Called whenever an
error
occurs and notifiesBlocObserver.onError
.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited