install method

Zone install()

Installs the capture hooks and returns a Zone whose print calls are intercepted. The caller should run their application inside this zone:

final capture = LogCapture();
final zone = capture.install();
zone.run(() => runApp(MyApp()));

Implementation

Zone install() {
  if (_installed) return Zone.current;
  _installed = true;

  // Capture FlutterError.onError
  _previousErrorHandler = FlutterError.onError;
  FlutterError.onError = _handleFlutterError;

  // Create a zone that intercepts print()
  return Zone.current.fork(
    specification: ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
        buffer.add(line);
        // Still forward to the parent so output appears in the console.
        parent.print(zone, line);
      },
    ),
  );
}