runZonedGuarded<R> static method

dynamic runZonedGuarded<R>(
  1. R body(),
  2. void onError(
    1. Object error,
    2. StackTrace stack
    )?, {
  3. Map<Object?, Object?>? zoneValues,
  4. ZoneSpecification? zoneSpecification,
})

Creates a new error handling zone with Sentry integration using runZonedGuarded.

This method provides automatic error reporting and breadcrumb tracking while allowing you to define a custom error handling zone. It wraps Dart's native runZonedGuarded function with Sentry-specific functionality.

This function automatically records calls to print() as Breadcrumbs and can be configured using SentryOptions.enablePrintBreadcrumbs.

Sentry.runZonedGuarded(() {
  WidgetsBinding.ensureInitialized();

  // Errors before init will not be handled by Sentry

  SentryFlutter.init(
    (options) {
    ...
    },
    appRunner: () => runApp(MyApp()),
  );
} (error, stackTrace) {
  // Automatically sends errors to Sentry, no need to do any
  // captureException calls on your part.
  // On top of that, you can do your own custom stuff in this callback.
});

Implementation

static runZonedGuarded<R>(
  R Function() body,
  void Function(Object error, StackTrace stack)? onError, {
  Map<Object?, Object?>? zoneValues,
  ZoneSpecification? zoneSpecification,
}) =>
    SentryRunZonedGuarded.sentryRunZonedGuarded(
      _hub,
      body,
      onError,
      zoneValues: zoneValues,
      zoneSpecification: zoneSpecification,
    );