A common use case might be managing AuthenticationState
. For simplicity, let's say we can represent AuthenticationState
via an enum
:
enum AuthenticationState { unknown, authenticated, unauthenticated }Copy to clipboardErrorCopied
There could be many reasons as to why the application's state could change from authenticated
to unauthenticated
. For example, the user might have tapped a logout button and requested to be signed out of the application. On the other hand, maybe the user's access token was revoked and they were forcefully logged out. When using Bloc
we can clearly trace how the application state got to a certain state.
Transition {
currentState: AuthenticationState.authenticated,
event: LogoutRequested,
nextState: AuthenticationState.unauthenticated
}Copy to clipboardErrorCopied
The above Transition
gives us all the information we need to understand why the state changed. If we had used a Cubit
to manage the AuthenticationState
, our logs would look like:
Change {
currentState: AuthenticationState.authenticated,
nextState: AuthenticationState.unauthenticated
}Copy to clipboardErrorCopied
This tells us that the user was logged out but it doesn't explain why which might be critical to debugging and understanding how the state of the application is changing over time.