|
| 1 | +--- |
| 2 | +title: Manual Setup |
| 3 | +sidebar_order: 1 |
| 4 | +description: "Learn how to set up the SDK manually." |
| 5 | +--- |
| 6 | + |
| 7 | +If you can't (or prefer not to) run the [automatic setup](/platforms/flutter/#install), you can follow the instructions below to configure your application manually. |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +<OnboardingOptionButtons |
| 12 | + options={[ |
| 13 | + 'error-monitoring', |
| 14 | + 'performance', |
| 15 | + 'profiling', |
| 16 | + ]} |
| 17 | +/> |
| 18 | + |
| 19 | +Sentry captures data by using an SDK within your application's runtime. These are platform-specific and allow Sentry to have a deep understanding of how your application works. |
| 20 | + |
| 21 | +```yml {filename:pubspec.yaml} |
| 22 | +dependencies: |
| 23 | + sentry_flutter: ^{{@inject packages.version('sentry.dart.flutter') }} |
| 24 | +``` |
| 25 | +
|
| 26 | +## Configure |
| 27 | +
|
| 28 | +Configuration should happen as early as possible in your application's lifecycle. |
| 29 | +
|
| 30 | +```dart {"onboardingOptions": {"performance": "8-10", "profiling": "11-14"}} |
| 31 | +import 'package:flutter/widgets.dart'; |
| 32 | +import 'package:sentry_flutter/sentry_flutter.dart'; |
| 33 | + |
| 34 | +Future<void> main() async { |
| 35 | + await SentryFlutter.init( |
| 36 | + (options) { |
| 37 | + options.dsn = '___PUBLIC_DSN___'; |
| 38 | + // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing. |
| 39 | + // We recommend adjusting this value in production. |
| 40 | + options.tracesSampleRate = 1.0; |
| 41 | + // The sampling rate for profiling is relative to tracesSampleRate |
| 42 | + // Setting to 1.0 will profile 100% of sampled transactions: |
| 43 | + // Note: Profiling alpha is available for iOS and macOS since SDK version 7.12.0 |
| 44 | + options.profilesSampleRate = 1.0; |
| 45 | + // Adds request headers and IP for users, |
| 46 | + // visit: https://docs.sentry.io/platforms/flutter/data-management/data-collected/ for more info |
| 47 | + options.sendDefaultPii = true; |
| 48 | + }, |
| 49 | + appRunner: () => runApp( |
| 50 | + SentryWidget( |
| 51 | + child: MyApp(), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ); |
| 55 | + |
| 56 | + // you can also configure SENTRY_DSN, SENTRY_RELEASE, SENTRY_DIST, and |
| 57 | + // SENTRY_ENVIRONMENT via Dart environment variable (--dart-define) |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +```dart {tabTitle:With custom zone} {"onboardingOptions": {"performance": "16-18", "profiling": "19-22"}} |
| 62 | +import 'package:flutter/widgets.dart'; |
| 63 | +import 'package:sentry_flutter/sentry_flutter.dart'; |
| 64 | +
|
| 65 | +Future<void> main() async { |
| 66 | + // The SDK creates it's own custom zone on web for automatic error and breadcrumb tracking on web. |
| 67 | + // This could lead to zone mismatch errors if you needed to call `WidgetsBinding.ensureInitialized()` before Sentry in a cusom zone. |
| 68 | + // With `Sentry.runZonedGuarded` you still get convenient auto error and breadcrumb tracking and can also call `WidgetsBinding.ensureInitialized()` before Sentry. |
| 69 | + Sentry.runZonedGuarded(() async { |
| 70 | + WidgetsBinding.ensureInitialized(); |
| 71 | +
|
| 72 | + // Errors before init will not be handled by Sentry |
| 73 | +
|
| 74 | + await SentryFlutter.init( |
| 75 | + (options) { |
| 76 | + options.dsn = '___PUBLIC_DSN___'; |
| 77 | + // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing. |
| 78 | + // We recommend adjusting this value in production. |
| 79 | + options.tracesSampleRate = 1.0; |
| 80 | + // The sampling rate for profiling is relative to tracesSampleRate |
| 81 | + // Setting to 1.0 will profile 100% of sampled transactions: |
| 82 | + // Note: Profiling alpha is available for iOS and macOS since SDK version 7.12.0 |
| 83 | + options.profilesSampleRate = 1.0; |
| 84 | + }, |
| 85 | + appRunner: () => runApp( |
| 86 | + SentryWidget( |
| 87 | + child: MyApp(), |
| 88 | + ), |
| 89 | + ), |
| 90 | + ); |
| 91 | + } (error, stackTrace) { |
| 92 | + // Automatically sends errors to Sentry, no need to do any |
| 93 | + // captureException calls on your part. |
| 94 | + // On top of that, you can do your own custom stuff in this callback. |
| 95 | + }); |
| 96 | +
|
| 97 | + // you can also configure SENTRY_DSN, SENTRY_RELEASE, SENTRY_DIST, and |
| 98 | + // SENTRY_ENVIRONMENT via Dart environment variable (--dart-define) |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Verify |
| 103 | + |
| 104 | +Verify that your app is sending events to Sentry by adding the following snippet, which includes an intentional error. You should see the error reported in Sentry within a few minutes. |
| 105 | + |
| 106 | +```dart |
| 107 | +import 'package:sentry/sentry.dart'; |
| 108 | +
|
| 109 | +try { |
| 110 | + throw Exception('Sentry Test Exception'); |
| 111 | +} catch (exception, stackTrace) { |
| 112 | + await Sentry.captureException( |
| 113 | + exception, |
| 114 | + stackTrace: stackTrace, |
| 115 | + ); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Next Steps |
| 120 | + |
| 121 | +- <PlatformLink to="/features">Learn about the features of Sentry's Flutter SDK</PlatformLink> |
| 122 | +- <PlatformLink to="/upload-debug/#when-to-upload/">Add readable stack traces to errors</PlatformLink> |
| 123 | +- <PlatformLink to="/tracing/instrumentation">Add performance instrumentation to your app</PlatformLink> |
0 commit comments