Back to Blog
Getting Started with StackWatch for Flutter
ST
StackWatch Team
Product Team
Learn how to integrate StackWatch into your Flutter application for comprehensive error tracking and crash reporting.
Prerequisites
- Flutter 3.0 or higher
- Dart 2.17 or higher
- A StackWatch account and project DSN
Step 1: Add the Dependency
Add StackWatch to your pubspec.yaml:
dependencies:
stackwatch_flutter: ^1.0.0
Step 2: Initialize StackWatch
Initialize StackWatch in your main.dart:
import 'package:stackwatch_flutter/stackwatch_flutter.dart';
Future<void> main() async {
await StackWatch.init(
StackWatchOptions(
dsn: 'your-dsn-here',
environment: 'production',
),
);
runApp(MyApp());
}
Step 3: Wrap Your App
Wrap your app with StackWatch's error boundary:
runApp(
StackWatchErrorBoundary(
child: MyApp(),
),
);
Capturing Navigation Events
Add the StackWatch navigator observer to track screen transitions:
MaterialApp(
navigatorObservers: [
StackWatchNavigatorObserver(),
],
// ...
)
Manual Error Capture
Capture errors manually when needed:
try {
await someOperation();
} catch (e, stackTrace) {
await StackWatch.captureException(e, stackTrace: stackTrace);
}
User Identification
Identify users for better error context:
StackWatch.setUser(
StackWatchUser(
id: user.id,
email: user.email,
username: user.name,
),
);
Your Flutter app is now configured to send errors and crashes to StackWatch!