CorrelationContext class

Manages correlation IDs for tracking operations across the application.

Correlation IDs help trace a single user action or request through multiple layers of the application (intents, state changes, effects, etc.).

This is particularly useful for:

  • Debugging complex flows
  • Tracing user actions in logs
  • Correlating errors with their originating actions
  • Performance monitoring

Example usage:

// At the start of a user action:
await CorrelationContext.runWithCorrelation(() async {
  // All code here will have access to the same correlation ID
  await controller.intent(LoginIntent());
});

// In any part of the code:
final id = CorrelationContext.current;
logger.info('Processing login', context: {'correlationId': id});

Constructors

CorrelationContext()

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

asContext → Map<String, String>?
Gets the correlation ID as a map entry for easy inclusion in context maps.
no setter
current → String?
Gets the current correlation ID from the zone, or null if not in a correlated context.
no setter

Static Methods

resetCounter() → void
Resets the internal counter (useful for testing).
runSyncWithCorrelation<T>(T fn(), {String? correlationId}) → T
Runs the given synchronous function with a new correlation ID.
runWithCorrelation<T>(Future<T> fn(), {String? correlationId}) → Future<T>
Runs the given function with a new correlation ID.