init static method

Future<void> init(
  1. FlutterOptionsConfiguration optionsConfiguration, {
  2. AppRunner? appRunner,
  3. @internal SentryFlutterOptions? options,
})

Initializes the Sentry Flutter SDK.

Unlike Sentry.init, this method creates the Flutter default integrations.

optionsConfiguration is a callback that allows you to configure the Sentry options. The SentryFlutterOptions should not be adjusted anywhere else than during init, so that's why they're not directly exposed outside of this method.

You can use the static members of Sentry from within other packages without the need of initializing it in the package; as long as they have been already properly initialized in the application package.

Implementation

static Future<void> init(
  FlutterOptionsConfiguration optionsConfiguration, {
  AppRunner? appRunner,
  @internal SentryFlutterOptions? options,
}) async {
  SentryScreenshotWidget.reset();
  options ??= SentryFlutterOptions();

  // ignore: invalid_use_of_internal_member
  sentrySetupStartTime ??= options.clock();

  if (options.platformChecker.hasNativeIntegration) {
    _native = createBinding(options);
  }

  final wrapper = PlatformDispatcherWrapper(PlatformDispatcher.instance);
  options.isMultiViewApp = wrapper.isMultiViewEnabled(options);

  // Flutter Web doesn't capture [Future] errors if using [PlatformDispatcher.onError] and not
  // the [runZonedGuarded].
  // likely due to https://github.com/flutter/flutter/issues/100277
  final bool isOnErrorSupported =
      !options.platformChecker.isWeb && wrapper.isOnErrorSupported(options);

  final bool isRootZone = options.platformChecker.isRootZone;

  // If onError is not supported and no custom zone exists, use runZonedGuarded to capture errors.
  final bool useRunZonedGuarded = !isOnErrorSupported && isRootZone;

  RunZonedGuardedOnError? runZonedGuardedOnError =
      useRunZonedGuarded ? _createRunZonedGuardedOnError() : null;

  // first step is to install the native integration and set default values,
  // so we are able to capture future errors.
  final defaultIntegrations = _createDefaultIntegrations(
    options,
    isOnErrorSupported,
  );
  for (final defaultIntegration in defaultIntegrations) {
    options.addIntegration(defaultIntegration);
  }

  await _initDefaultValues(options);

  await Sentry.init(
    (o) {
      assert(options == o);
      return optionsConfiguration(o as SentryFlutterOptions);
    },
    appRunner: appRunner,
    // ignore: invalid_use_of_internal_member
    options: options,
    // ignore: invalid_use_of_internal_member
    callAppRunnerInRunZonedGuarded: useRunZonedGuarded,
    // ignore: invalid_use_of_internal_member
    runZonedGuardedOnError: runZonedGuardedOnError,
  );

  if (_native != null) {
    // ignore: invalid_use_of_internal_member
    SentryNativeProfilerFactory.attachTo(Sentry.currentHub, _native!);
  }

  // Insert it at the start of the list, before the Dart Exceptions that are set in Sentry.init
  // so we can identify Flutter exceptions first.
  options.prependExceptionTypeIdentifier(FlutterExceptionTypeIdentifier());
}