Integrate Sentry Error Reporting In Flutter App

by Jule 48 views
Integrate Sentry Error Reporting In Flutter App

Integrate Sentry Error Reporting in Flutter App

Why Sentry?

Sentry helps us track, fix, and learn from any exception in our Flutter app. It automatically captures Dart exceptions, native crashes, and provides a minimal release and device context. Let's dive into how to integrate it.

Getting Started

1. Add Dependency

First, add the sentry_flutter dependency to your pubspec.yaml:

dependencies:
sentry_flutter: ^8.12.0

2. Initialize Sentry

Replace the top of your main() function in lib/main.dart with the following:

Future<void> main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Hive.initFlutter();
 await KeyService().getCipher();

 const sentryDsn = String.fromEnvironment('SENTRY_DSN', defaultValue: '');
 if (sentryDsn.isEmpty) {
 runApp(const ProviderScope(child: CosmicMatchApp()));
 return;
 }

 await SentryFlutter.init(
 (options) {
 options.dsn = sentryDsn;
 options.environment = kReleaseMode ? 'release' : 'debug';
 options.release = 'com.cosmicmatch.cosmic_match@${packageInfo.version}+${packageInfo.buildNumber}';
 options.tracesSampleRate = 0.0;
 options.sendDefaultPii = false;
 options.attachScreenshot = false;
 options.attachViewHierarchy = false;
 },
 appRunner: () => runApp(const ProviderScope(child: CosmicMatchApp()))
 );
}

3. Wire DSN

Compile-time DSN wiring:\n

  • Android: Nothing needed, Flutter picks up --dart-define values.
  • CI Build Job: Pass the DSN to Flutter build:
- run: flutter build apk --release --dart-define=SENTRY_DSN=${{ secrets.SENTRY_DSN }}
  • Repo Secret: Add SENTRY_DSN as a repo secret.

What's Captured?

  • Unhandled Dart exceptions and Flutter framework errors.
  • Native crashes (iOS & Android).
  • Breadcrumbs: navigation, HTTP (if any), device orientation.

What's NOT Captured?

  • No PII.
  • No screenshots / view hierarchies.
  • No game state (board positions, player progress).

Privacy Policy

Add a note to your SECURITY.md and future privacy policy: "Anonymous crash reports are sent to Sentry. No personal data, no game state."

Acceptance Criteria

  • [ ] Dependency added, app builds.
  • [ ] SentryFlutter.init gated on non-empty compile-time DSN.
  • [ ] DSN flows from CI secret → --dart-define → runtime.
  • [ ] A forced debug-only crash shows up in Sentry.
  • [ ] No release build leaks the DSN into build logs.

Happy coding, guys! Let's make our Flutter app even better with Sentry.