BlocNotificationMixin<State, N> mixin
The BlocNotificationMixin is a Flutter Bloc mixin that provides a simple
notification mechanism for handling UI-related actions separately from the
state. By using this mixin, you can fire notifications from the Bloc
and
handle them in the UI layer using a callback mechanism,
without having to manage them in the state.
To use the BlocNotificationMixin, simply import the flutter_bloc
package, and apply the mixin to your Bloc
/ Cubit
:
class MyBloc extends Bloc<MyEvent, MyState> with BlocNotificationMixin<MyState, MyNotification> {
// ...
}
The BlocNotificationMixin defines a Stream
of notifications that can be
listened to by the UI layer using the notification getter. To fire a
notification from the Bloc
/ Cubit
, simply call the notify method with
the notification payload:
notify(MyNotification(payload));
Where MyNotification
is a class that represents the notification and
payload is the data associated with the notification.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether the bloc is closed.
no setterinherited
- isNotificationClosed → bool
-
Whether the notification stream is closed.
no setter
-
notification
→ Stream<
N> -
The notification stream
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → State
-
The current state.
no setterinherited
-
stream
→ Stream<
State> -
The current stream of states.
no setterinherited
Methods
-
addError(
Object error, [StackTrace? stackTrace]) → void -
Reports an
error
which triggers onError 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.
-
emit(
State state) → void -
Updates the
state
to the providedstate
. emit does nothing if thestate
being emitted is equal to the currentstate
.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notify(
N notification) → void -
Send a notification to listeners, this will call onNotification on this
class and optionally on
BlocObserver
ifBlocObserver
implement NotificationObserverMixin. By default, calling notify after it is closed will throw StateError and trigger onError. -
onChange(
Change< State> change) → void -
Called whenever a
change
occurs with the givenchange
. Achange
occurs when a newstate
is emitted. onChange is called before thestate
of thecubit
is updated. onChange is a great spot to add logging/analytics for a specificcubit
.inherited -
onError(
Object error, StackTrace stackTrace) → void -
Called whenever an
error
occurs and notifiesBlocObserver.onError
.inherited -
onNotification(
BlocNotification< N> blocNotification) → void -
Function called when there's new BlocNotification fired from
BlocBase
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited