runZoned<T> method
Runs body in a zone that can forward print() and uncaught errors into
this console controller.
This is useful for wiring the controller into app-level runners so diagnostics land in the built-in console without manually threading the controller through business logic.
Implementation
Future<T> runZoned<T>(
FutureOr<T> Function() body, {
bool capturePrint = true,
bool captureErrors = true,
String printLevel = 'debug',
}) {
final completer = Completer<T>();
final zoneSpecification = capturePrint
? ZoneSpecification(
print: (self, parent, zone, line) {
add(line, level: printLevel);
},
)
: null;
runZonedGuarded(
() async {
try {
final result = await body();
if (!completer.isCompleted) {
completer.complete(result);
}
} catch (error, stackTrace) {
if (captureErrors) {
exception(error, stackTrace);
}
if (!completer.isCompleted) {
completer.completeError(error, stackTrace);
}
}
},
(error, stackTrace) {
if (captureErrors) {
exception(error, stackTrace);
}
},
zoneSpecification: zoneSpecification,
);
return completer.future;
}