Dio Integration

The sentry_dio library provides Dio support for Sentry using the HttpClientAdapter. It is able to collect breadcrumbs, run performance monitoring for HTTP requests, and capture events for failed requests.

The source can be found on GitHub.

Install

To add the Dio integration, add the sentry_dio dependency.

pubspec.yaml
Copied
dependencies:
  sentry: ^6.16.1
  sentry_dio: ^6.16.1
  dio: ^4.0.0

Configure

Configuration should happen as early as possible in your application's lifecycle.

Copied
import 'package:sentry_dio/sentry_dio.dart';
import 'package:sentry/sentry.dart';

Future<void> main() async {
  await Sentry.init(
    (options) {
      options.dsn = 'https://examplePublicKey@o0.ingest.sentry.io/0';
    },
    appRunner: initApp, // Init your App.
  );
}

final dio = Dio();

// This *must* be the last initialization step of the Dio setup.
dio.addSentry(...);

Reporting Bad HTTP Requests as Errors

The Interceptors can also catch exceptions that may occur during requests — for example DioError. This is currently an opt-in feature. The following example shows how to enable it.

Copied
import 'package:sentry_dio/sentry_dio.dart';

final dio = Dio();

dio.addSentry(captureFailedRequests: true);

final response = await dio.get<String>('https://wrong-url.dev/');

Performance Monitoring for HTTP Requests

The Dio integration starts a span out of the active span bound to the scope for each HTTP request. This is currently enabled by default. The following example shows how to disable it.

Copied
import 'package:sentry_dio/sentry_dio.dart';

final dio = Dio();

dio.addSentry(networkTracing: false);
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").