runZoned<R> function Null safety

R runZoned<R>(
  1. R body(
    1. {Map<Object?, Object?>? zoneValues,
    2. ZoneSpecification? zoneSpecification,
    3. @Deprecated("Use runZonedGuarded instead") Function? onError}

    Runs body in its own zone.

    Creates a new zone using Zone.fork based on zoneSpecification and zoneValues, then runs body in that zone and returns the result.

    Example use:

    var secret = "arglebargle"; // Or a random generated string.
    var result = runZoned(
        () async {
          await Future.delayed(Duration(seconds: 5), () {
            print("${Zone.current[#_secret]} glop glyf");
        zoneValues: {#_secret: secret},
            ZoneSpecification(print: (Zone self, parent, zone, String value) {
          if (value.contains(Zone.current[#_secret] as String)) {
            value = "--censored--";
          parent.print(zone, value);
    secret = ""; // Erase the evidence.
    await result; // Wait for asynchronous computation to complete.

    The new zone intercepts print and stores a value under the private symbol #_secret. The secret is available from the new Zone object, which is the Zone.current for the body, and is also the first, self, parameter to the print handler function.


    R runZoned<R>(R body(),
        {Map<Object?, Object?>? zoneValues,
        ZoneSpecification? zoneSpecification,
        @Deprecated("Use runZonedGuarded instead") Function? onError}) {
      checkNotNullable(body, "body");
      if (onError != null) {
        // TODO: Remove this when code have been migrated off using [onError].
        if (onError is! void Function(Object, StackTrace)) {
          if (onError is void Function(Object)) {
            var originalOnError = onError;
            onError = (Object error, StackTrace stack) => originalOnError(error);
          } else {
            throw ArgumentError.value(onError, "onError",
                "Must be Function(Object) or Function(Object, StackTrace)");
        return runZonedGuarded(body, onError,
            zoneSpecification: zoneSpecification, zoneValues: zoneValues) as R;
      return _runZoned<R>(body, zoneValues, zoneSpecification);