current property

Context get current

Gets the current Context from the current Zone, or the global Context if none exists.

The search order is: current Zone, global current Context, root Context.

In asynchronous Dart code, the Zone-based context is preferred as it propagates correctly across asynchronous boundaries.

Implementation

static Context get current {
  return Zone.current[_zoneKey] as Context? ??
      _currentContext ??
      Context.root;
}
  1. @Deprecated('Use Context.run() or Context.runSync() to ensure correct asynchronous propagation via Zones. This setter will be removed in v1.0.0.')
set current (Context newContext)

Sets the global current Context.

This updates the global current Context but does not affect Zone-specific contexts.

@deprecated Use Context.run or Context.runSync to activate a context within a Zone. Setting this static field is unreliable in complex asynchronous workflows as it does not propagate through Zones.

Implementation

@Deprecated(
    'Use Context.run() or Context.runSync() to ensure correct asynchronous propagation via Zones. This setter will be removed in v1.0.0.')
static set current(Context newContext) {
  _currentContext = newContext;
}