How to setup sentry.io on flutter

Phong Yew Tong
2 min readSep 12, 2022

--

flutter sentry.io

Subscribe to TLDRStories.com to get your daily dose of tech news, condensed into a few stories.

Sentry.io is an error and performance monitoring tool for your app and web. I choose sentry.io over firebase crashlytics because it has web tracking.

If you are using flutter choose sentry_flutter instead of sentry package which is recommended on sentry doc, don't mix them up. I waste a little time trying to set sentry up and somehow it works but not everything.

flutter pub add sentry_flutter

In your main

import 'package:sentry_flutter/sentry_flutter.dart';

Future<void> main() async {
runZonedGuarded(() async {
await SentryFlutter.init(
(options) {
options.dsn = 'https://example@sentry.io/add-your-dsn-here';
},
);

runApp(MyApp());
}, (exception, stackTrace) async {
await Sentry.captureException(exception, stackTrace: stackTrace);
});
}

After creating a sentry.io account and project, you can go to Settings >> Client Keys to get the DSN.

sentry.io dashboard

In your code, throw an Exception anywhere to test if your setup works. In this case, I insert it in a button pressed function.

void onItemTapped(int idx) async {
setState(() => _selectedIdx = idx);
throw Exception('throw first error');
}

If your setup works, you should see your exception in sentry.io dashboard

sentry.io dashboard

Sentry.io basic setup is done! Wooooohooo!

Additional Setup

You can filter out your production and debug environment error in sentry.io.

sentry.io debug mode

Just insert kDebugMode.

import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:flutter/foundation.dart' show kDebugMode;
Future<void> main() async {
runZonedGuarded(() async {
await SentryFlutter.init(
(options) {
options.dsn = 'https://example@sentry.io/add-your-dsn-here';
option.debug = kDebugMode
},
);

runApp(MyApp());
}, (exception, stackTrace) async {
await Sentry.captureException(exception, stackTrace: stackTrace);
});
}

Drop me some claps and follow me if this article helps you, it motivates me to create more! Thank you, everyone! ๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘

Subscribe to TLDRStories.com to get your daily dose of tech news, condensed into a few stories.

--

--

No responses yet