Sentry for Dart

Learn how to set up Sentry in your Dart app, capture your first errors and traces, and view them in Sentry.

Agent-Assisted Setup
Use curl to download, read and follow https://skills.sentry.dev/instrument to set up the Sentry Dart SDK.
Your agent will set up Sentry in your Dart app automatically. Works with Cursor, Claude Code, Codex, and more.View docs ↗
Install the full plugin

Install the Sentry plugin to give your assistant every skill. See the installation docs for more details.

Copied
npx @sentry/agent-plugin install

You need:

Add the Sentry Dart SDK to your pubspec.yaml:

pubspec.yaml
Copied
dependencies:
  sentry: ^10.3.0

Choose the features you want to configure, and this guide will show you how:

Want to learn more about these features?
  • Issues (always enabled): Sentry's core error monitoring product that automatically reports errors, uncaught exceptions, and crashes. If you have something that looks like an exception, Sentry can capture it.
  • Tracing: Track software performance while seeing the impact of errors across multiple systems. For example, distributed tracing allows you to follow a request from the frontend to the backend and back.
  • Logs: Centralize and analyze your application logs to correlate them with errors and performance issues. Search, filter, and visualize log data to understand what's happening in your applications.
  • Application Metrics (always enabled): Track and analyze custom application metrics, such as response times and database query durations, to understand trends and patterns in your application's performance and behavior over time.

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

Import and initialize the SDK in your app's entry point:

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

Future<void> main() async {
  await Sentry.init((options) {
    options.dsn = '___PUBLIC_DSN___';
    // Adds request headers and IP for users, for more info visit:
    // https://docs.sentry.io/platforms/dart/data-management/data-collected/
    options.sendDefaultPii = true;
    // ___PRODUCT_OPTION_START___ performance
    // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
    // We recommend adjusting this value in production.
    options.tracesSampleRate = 1.0;
    // ___PRODUCT_OPTION_END___ performance
  });

  // you can also configure SENTRY_DSN, SENTRY_RELEASE, SENTRY_DIST, and
  // SENTRY_ENVIRONMENT via Dart environment variable (--dart-define)
}

Let's test your setup and confirm that data reaches your Sentry project.

To verify that Sentry captures errors and creates issues in your Sentry project, add this intentional error to your application:

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

try {
  throw StateError('Sentry Test Error');
} catch (exception, stackTrace) {
  await Sentry.captureException(
    exception,
    stackTrace: stackTrace,
  );
}

To test your tracing configuration, create a custom transaction and span:

Copied
final transaction = Sentry.startTransaction('test-transaction', 'task');
final span = transaction.startChild('test-span');
await span.finish();
await transaction.finish();

Logs are enabled by default. Use the Sentry logger to send structured logs from anywhere in your application:

Copied
Sentry.logger.info('User clicked checkout button');

Sentry.logger.info('Order completed', attributes: {
  'order_id': SentryAttribute.string('12345'),
  'total': SentryAttribute.double(99.99),
});

Sentry.logger.warn('Warning message');
Sentry.logger.error('Error occurred');

Metrics are enabled by default. Send test metrics from your app to verify that metrics are arriving in Sentry:

Copied
Sentry.metrics.count('checkout.failed', 1);
Sentry.metrics.gauge('queue.depth', 42);
Sentry.metrics.distribution('cart.amount_usd', 187.5);

Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).

Need help locating captured data in your Sentry project?
  • Open the Issues page and select an error from the issues list to view the full details and context of this error. For more details, see the Issue Details documentation.
  • Open the Traces page and select a trace to reveal more information about each span, its duration, and any errors. For an interactive UI walkthrough, click here.
  • Open the Logs page and filter by service, environment, or search keywords to view log entries from your application. For an interactive UI walkthrough, click here.
  • Open the Application Metrics page to view and analyze your metrics. For more details, see this interactive walkthrough.

At this point, you should have integrated Sentry into your Dart application and should already be sending data to your Sentry project.

Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:

Are you having problems setting up the SDK?
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").