zone function

  1. @experimental
Zone zone([
  1. Context? context
])

Returns a new Zone such that the given context will be automatically attached and detached for any function that runs within the zone.

Implementation

@experimental
Zone zone([Context? context]) => Zone.current.fork(
        specification: ZoneSpecification(run: <R>(self, parent, zone, fn) {
      // Only attach the context when delegating this zone's run, not any
      // potential child. Otherwise, the child zone's current context would
      // end up being the outermost zone attached context.
      if (self == zone) {
        final token = Context.attach(context ?? _currentContext(zone), zone);
        final result = parent.run(zone, fn);
        if (result is Future) {
          result.whenComplete(() {
            Context.detach(token, zone);
          });
        } else {
          Context.detach(token, zone);
        }
        return result;
      }
      return parent.run(zone, fn);
    }));