Sentry is NOT reporting error inside Flutter

Viewed 2241

I have my Sentry setup like this:

void main() => runZonedGuarded(() {
  runApp(MyApp());
}, (Object error, StackTrace stackTrace) {
  reportError(error, stackTrace);
});

and related functions

final SentryClient sentry = new SentryClient(dsn: '<my-dsn>');


Future<void> reportError(dynamic error, dynamic stackTrace) async {
  sentry.captureException(
    exception: error,
    stackTrace: stackTrace,
  );
}

I added throw Exception("my-error") inside a widget's build method, I can't see the error is showing on the Sentry web console.

I create a single file to throw exception and sentry capture, and I do see sentry is reporting the error.

Something must wrong with runZonedGuarded.

4 Answers

Check in your sentry dashboard if you are using the free version and if the monthly quota hasnt been surpassed. If that is the case you will not receive any events.

  1. you must make the func async to send error to sentry console
  2. be sure import this file for mobile app:
    import 'package:sentry/io_client.dart';
    E.g:

main.dart

import 'package:sentry/io_client.dart';
final SentryClient sentry = new SentryClient(dsn: YOUR_DSN);

main() async {
  try {
    throw new StateError('This is a Dart exception.');
  } catch(error, stackTrace) {
    await sentry.captureException(
      exception: error,
      stackTrace: stackTrace,
    );
  }
}

HomeScreen

floatingActionButton: FloatingActionButton(
          onPressed: () async{

            throw new StateError('This is a Dart exception.');

          },

After a number of Sentry setups that didn't seem to work right, I arrived at this one that works:

Future<void> main() async {
  final sentry = Sentry.SentryClient(Sentry.SentryOptions(dsn: '[Add dsn URI here]'));

  runZonedGuarded(() {
    WidgetsFlutterBinding.ensureInitialized();

    FlutterError.onError = (FlutterErrorDetails errorDetails) {
      sentry.captureException(
        errorDetails.exception,
        stackTrace: errorDetails.stack,
      );
    };

    runApp(MyApp());
  }, (Object error, StackTrace stackTrace) {
    sentry.captureException(
      error,
      stackTrace: stackTrace,
    );
  });
}
Related