withContext<T> method

Future<T> withContext<T>(
  1. String transactionId,
  2. Map<String, dynamic> attributes,
  3. Future<T> operation()
)

Run code with a specific transaction context

Implementation

Future<T> withContext<T>(
  String transactionId,
  Map<String, dynamic> attributes,
  Future<T> Function() operation,
) async {
  final zoneContexts = Map<String, TransactionContext>.from(_contexts);
  final zoneStack = List<String>.from(_contextStack);

  return await runZoned(() async {
    final context = TransactionContext(
      transactionId: transactionId,
      attributes: attributes,
      parent: currentContext,
    );

    pushContext(context);
    try {
      return await operation();
    } finally {
      popContext();
    }
  }, zoneValues: {_zoneContextsKey: zoneContexts, _zoneStackKey: zoneStack});
}