Sentry Dart SDK

repository·main·Indexed 21 days ago

https://github.com/getsentry/sentry-dart

A modular suite of SDKs providing error tracking, performance monitoring, and observability for Dart and Flutter applications. Includes the core sentry package for CLI, Server, and AngularDart, as well as specialized integrations for Dio (sentry_dio), drift (sentry_drift), dart.io.File (sentry_file), and firebase_remote_config.

Tokens
19.9K
Snippets
70
Records
83
Agent score
74%

What's inside sentry-dart

  1. Overview of Sentry SDK for Dart and Flutter

    main

    The sentry-dart repository provides a suite of SDKs for error tracking, performance monitoring, and observability in Dart and Flutter applications. It is composed of several specialized packages to support different ecosystems and libraries:

    • Core SDKs:

      • sentry: The base Dart SDK.
      • sentry_flutter: Specialized support for Flutter applications.
      • sentry_logging: Integration for logging frameworks.
    • Integration Packages:

      • sentry_dio: For the dio HTTP client.
      • sentry_file: For file-related error tracking.
      • sentry_sqflite: For sqflite database integration.
      • sentry_drift: For drift database integration.
      • sentry_hive: For hive database integration.
      • sentry_isar: For isar database integration.
      • sentry_link: For link/navigation tracking.
  2. Understand Sentry SDK release types

    main

    Sentry releases updates using three distinct tiers to balance feature velocity with stability:

    • Pre-release: Alpha, beta, or RC versions used for testing large changes or new major versions.
    • Latest: Continuous releases from the main branch. These are considered safe for most teams and undergo internal quality gates.
    • Stable: Releases promoted from Latest after they have demonstrated high stability and adoption in production environments. These are marked with a Stable suffix on the GitHub releases page.
  3. Integrate Sentry with firebase_remote_config

    main

    The sentry_firebase_remote_config package allows you to track changes to Firebase boolean values as feature flags in Sentry.io. This integration helps you monitor how remote configuration changes impact your application's error rates and performance.

    Prerequisites

    1. A Sentry.io account and a DSN.
    2. The firebase_remote_config package installed in your project.
    3. The sentry_flutter (or sentry) package installed in your project.

    Setup Steps

    1. Initialize Firebase in your application.
    2. Create an instance of FirebaseRemoteConfig.
    3. Initialize the Sentry SDK using SentryFlutter.init (or Sentry.init for pure Dart).
    4. Create a SentryFirebaseRemoteConfigIntegration instance, passing your FirebaseRemoteConfig instance.
    5. Add the integration to your Sentry options using options.addIntegration().

    Configuration Options

    When creating the SentryFirebaseRemoteConfigIntegration, you can configure the activateOnConfigUpdated property:

    • activateOnConfigUpdated: (bool) Determines if the integration should automatically call remoteConfig.activate() when the configuration is updated. By default, this is true. If you manage activation manually, set this to false to avoid conflicts.
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter/material.dart';
    import 'package:sentry_flutter/sentry_flutter.dart';
    import 'package:sentry_firebase_remote_config/sentry_firebase_remote_config.dart';
    import 'firebase_options.dart';
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    
      final remoteConfig = FirebaseRemoteConfig.instance;
      await remoteConfig.setConfigSettings(RemoteConfigSettings(
        fetchTimeout: const Duration(minutes: 1),
        minimumFetchInterval: const Duration(hours: 1),
      ));
    
      await SentryFlutter.init(
        (options) {
          options.dsn = 'https://example@sentry.io/add-your-dsn-here';
    
          final sentryFirebaseRemoteConfigIntegration = SentryFirebaseRemoteConfigIntegration(
            firebaseRemoteConfig: remoteConfig,
            // Don't call `await remoteConfig.activate();` when firebase config is updated. Per default this is true.
            activateOnConfigUpdated: false,
          );
          options.addIntegration(sentryFirebaseRemoteConfigIntegration);
        },
      );
    
      runApp(const RemoteConfigApp());
    }
  4. Catching errors in Isolates

    main
    While the SDK automatically captures errors in the main Isolate (for non-Web apps) and errors caught via runZonedGuarded, you must manually add an error listener for custom Isolates. Use isolate.addSentryErrorListener() to ensure errors in those isolates are reported to Sentry.
  5. Customize iOS launch screen assets

    main

    To change the image displayed during the app's launch on iOS, you can either replace the image files directly in the packages/flutter/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a visual approach.

    Using Xcode:

    1. Open your Flutter project's iOS workspace by running open ios/Runner.xcworkspace in your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch images.
    open ios/Runner.xcworkspace
  6. Integrate Sentry with the Dio package

    main

    To monitor HTTP requests, performance tracing, and errors in your dio networking layer, use the sentry_dio package.

    Setup Steps

    1. Sign up for a Sentry.io account and obtain your DSN.
    2. Install the sentry and sentry_dio packages via pub.dev.
    3. Initialize the Sentry SDK using Sentry.init.
    4. Call dio.addSentry() on your Dio instance.

    Critical Requirement

    dio.addSentry() must be the last initialization step of your Dio setup. If you call it earlier, subsequent Dio configuration steps might overwrite the Sentry configuration.

    Features

    Depending on your configuration, this integration provides:

    • Performance tracing: Tracks the duration and lifecycle of HTTP requests.
    • HTTP breadcrumbs: Automatically logs HTTP activity to the Sentry breadcrumb trail.
    • Automatic error capturing: Captures exceptions resulting from invalid HTTP status codes or parsing errors.
    import 'package:sentry/sentry.dart';
    import 'package:sentry_dio/sentry_dio.dart';
    import 'package:dio/dio.dart';
    
    Future<void> main() async {
      await Sentry.init(
        (options) {
          options.dsn = 'https://example@sentry.io/example';
        },
        appRunner: initDio, // Init your App.
      );
    }
    
    void initDio() {
      final dio = Dio();
      /// This *must* be the last initialization step of the Dio setup, otherwise
      /// your configuration of Dio might overwrite the Sentry configuration.
      dio.addSentry();
    }