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: ^7.4.0
  sentry_dio: ^7.4.0
  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.

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

final dio = Dio();

dio.addSentry();

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

This is an opt-out feature. The following example shows how to disable it:

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

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

Performance Monitoring for HTTP Requests

The Dio integration starts a span out of the active span bound to the scope for each HTTP request.

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

final dio = Dio();

dio.addSentry();
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").