exampleMainSentryWrapper function

void exampleMainSentryWrapper()

APPROACH 1: Using Sentry's recommended SentryFlutter.init() wrapper (RECOMMENDED) This is the RECOMMENDED approach as it lets Sentry manage error handlers

This approach uses Sentry's appRunner pattern which automatically:

  • Sets up error handlers (FlutterError.onError, PlatformDispatcher.onError)
  • Runs your app in a proper error zone
  • Captures all uncaught errors

IMPORTANT: When using this approach, you MUST:

  1. Create a SentryErrorReporter implementation (see below)
  2. Call configureErrorReporting() with managesOwnErrorHandlers: true
  3. Call appInitErrorHandling() to set up Logger integration
  4. Call SentryFlutter.init() with appRunner

Example main.dart:

Implementation

void exampleMainSentryWrapper() async {
  /*
  import 'package:flutter/widgets.dart';
  import 'package:sentry_flutter/sentry_flutter.dart';
  import 'package:dreamic/dreamic.dart';

  // Simple Sentry reporter for use with SentryFlutter.init
  class SentryErrorReporter implements ErrorReporter {
    final String dsn;

    SentryErrorReporter({required this.dsn});

    @override
    Future<void> initialize() async {
      // ⚠️ CRITICAL: Must be empty (no-op) when using SentryFlutter.init with appRunner
      //
      // WHY: appInitErrorHandling() calls this initialize() method
      // Then SentryFlutter.init() is called in main with appRunner
      // If this method also initialized Sentry, it would initialize TWICE!
      //
      // RULE: When using SentryFlutter.init with appRunner, initialize() MUST be empty
    }

    @override
    void recordError(Object error, StackTrace? stackTrace) {
      Sentry.captureException(error, stackTrace: stackTrace);
    }

    @override
    void recordFlutterError(FlutterErrorDetails details) {
      Sentry.captureException(details.exception, stackTrace: details.stack);
    }
  }

  Future<void> main() async {
    WidgetsFlutterBinding.ensureInitialized();

    // STEP 1: Configure Dreamic to use Sentry
    configureErrorReporting(
      ErrorReportingConfig.customOnly(
        reporter: SentryErrorReporter(dsn: 'https://your-dsn@sentry.io/project-id'),
        managesOwnErrorHandlers: true,  // Sentry sets up error handlers
        enableOnWeb: true,
      ),
    );

    // STEP 2: Initialize error handling (sets Logger integration)
    await appInitErrorHandling();

    // STEP 3: Initialize Sentry with appRunner
    await SentryFlutter.init(
      (options) {
        options.dsn = 'https://your-dsn@sentry.io/project-id';
        options.environment = AppConfigBase.environmentType.value;
        options.release = await AppConfigBase.getReleaseId();
        options.tracesSampleRate = 1.0;
      },
      appRunner: () => appRunIfValidVersion(() => MyApp()),
    );
  }
  */
}