Flutter error tracking installation

Planned features

We currently don't support the following features:

  • No de-obfuscating stacktraces from obfuscated builds (--obfuscate and --split-debug-info) for Dart code
  • No de-obfuscating stacktraces when isMinifyEnabled is enabled for Java/Kotlin code
  • No Source code context associated with an exception
  • No native iOS exception capture
  • No native C/C++ exception capture on Android (Java/Kotlin only)
  • No background isolate error capture
  • No Flutter web support

These features will be added in future releases.

  1. Install PostHog Flutter SDK

    Required
    SDK version requirement

    A minimum SDK version of 5.9.0 is required, but we recommend keeping up to date with the latest version to ensure you have all of error tracking's features.

    Manual installation

    First, add posthog_flutter to your pubspec.yaml:

    pubspec.yaml
    # rest of your code
    dependencies:
    flutter:
    sdk: flutter
    posthog_flutter: ^5.0.0
    # rest of your code

    Then complete the manual setup for each platform:

    Android setup

    Add your PostHog configuration to your AndroidManifest.xml file located in the android/app/src/main:

    android/app/src/main/AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name">
    <application>
    <!-- ... other configuration ... -->
    <meta-data android:name="com.posthog.posthog.AUTO_INIT" android:value="false" />
    </application>
    </manifest>

    You'll also need to update the minimum Android SDK version to 21 in android/app/build.gradle:

    android/app/build.gradle
    // rest of your config
    defaultConfig {
    minSdkVersion 21
    // rest of your config
    }
    // rest of your config

    iOS setup

    Add your PostHog configuration to the Info.plist file located in the ios/Runner directory:

    ios/Runner/Info.plist
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <!-- rest of your configuration -->
    <key>com.posthog.posthog.AUTO_INIT</key>
    <false/>
    </dict>
    </plist>

    You'll need to set the minimum platform version to iOS 13.0 in your Podfile:

    ios/Podfile
    platform :ios, '13.0'
    # rest of your config

    Dart setup

    Then setup the SDK manually:

    Dart
    import 'package:flutter/material.dart';
    import 'package:posthog_flutter/posthog_flutter.dart';
    Future<void> main() async {
    // init WidgetsFlutterBinding if not yet
    WidgetsFlutterBinding.ensureInitialized();
    final config = PostHogConfig('<ph_project_api_key>');
    config.debug = true;
    config.captureApplicationLifecycleEvents = true;
    // or EU Host: 'https://eu.i.posthog.com'
    config.host = 'https://us.i.posthog.com';
    await Posthog().setup(config);
    runApp(MyApp());
    }
  2. Set up exception autocapture

    Recommended
    Client-side configuration only

    This configuration is client-side only. Support for remote configuration in the error tracking settings will be added in a future release.

    You can autocapture exceptions by configuring the errorTrackingConfig when setting up PostHog:

    Dart
    final config = PostHogConfig('<ph_project_api_key>');
    // Enable exception autocapture
    config.errorTrackingConfig.captureFlutterErrors = true;
    config.errorTrackingConfig.capturePlatformDispatcherErrors = true;
    config.errorTrackingConfig.captureIsolateErrors = true;
    config.errorTrackingConfig.captureNativeExceptions = true; // Android only
    config.errorTrackingConfig.captureSilentFlutterErrors = false;
    await Posthog().setup(config);

    Configuration options:

    • captureFlutterErrors: Captures Flutter framework errors (FlutterError.onError)
    • capturePlatformDispatcherErrors: Captures Dart runtime errors (PlatformDispatcher.onError)
    • captureIsolateErrors: Captures errors from main isolate
    • captureNativeExceptions: Captures native exceptions (Android only - Java/Kotlin exceptions)
    • captureSilentFlutterErrors: Captures Flutter errors that are marked as silent (default: false)
  3. Manually capture exceptions

    Optional

    Basic usage

    You can manually capture exceptions using the captureException method:

    Dart
    try {
    // Your awesome code that may throw
    await someRiskyOperation();
    } catch (exception, stackTrace) {
    // Capture the exception with PostHog
    await Posthog().captureException(
    error: exception,
    stackTrace: stackTrace,
    properties: {
    'user_action': 'button_press',
    'feature_name': 'data_sync',
    },
    );
    }

    This is helpful if you've built your own error handling logic or want to capture exceptions that are handled by your application code.

    Error tracking configuration

    You can configure error tracking behavior when setting up PostHog:

    Dart
    final config = PostHogConfig('<ph_project_api_key>');
    // Configure error tracking
    config.errorTrackingConfig.inAppIncludes = ['package:your_app'];
    config.errorTrackingConfig.inAppExcludes = ['package:third_party_lib'];
    config.errorTrackingConfig.inAppByDefault = true;
    await Posthog().setup(config);

    Configuration options:

    • inAppIncludes: List of package names to be considered inApp frames (takes precedence over excludes)
    • inAppExcludes: List of package names to be excluded from inApp frames
    • inAppByDefault: Whether frames are considered inApp by default when their origin cannot be determined

    inApp frames are stack trace frames that belong to your application code (as opposed to third-party libraries or system code). These are highlighted in the PostHog error tracking interface to help you focus on the relevant parts of the stack trace.

  4. Verify error tracking

    Checkpoint
    Confirm events are being sent to PostHog

    Before proceeding, let's make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.


    Activity feed with events
    Check for exceptions in PostHog

Community questions

Was this page useful?

Questions about this page? or post a community question.