run<R> method Null safety

Future<Progress> run<R>(
  1. R body(
      ),
    1. {Progress? progress}
    )

    Run dcli code in a zone which traps calls to print and printerr redirecting them to the passed progress.

    Implementation

    Future<Progress> run<R>(R Function() body, {Progress? progress}) async {
      progress ??= Progress.devNull();
    
      /// overload printerr so we can trap it.
      final zoneValues = <String, DCliZonePrintErr>{
        'printerr': (line) {
          if (line != null) {
            progress!.addToStderr(line);
          }
        }
      };
    
      // ignore: flutter_style_todos
      /// TODO: we need to some how await this.
      runZonedGuarded(
        body,
        (e, st) {},
        zoneValues: zoneValues,
        zoneSpecification: ZoneSpecification(
          print: (self, parent, zone, line) => progress!.addToStdout(line),
        ),
      );
    
      return progress;
    }