exception_catcher 0.1.1 copy "exception_catcher: ^0.1.1" to clipboard
exception_catcher: ^0.1.1 copied to clipboard

Flutter exception collection and reporter.

example/lib/main.dart

import 'package:exception_catcher/exception_catcher.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  await ExceptionCatcher.runAppGuarded(
    () async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(const ExampleApp());
    },
    config: ExceptionCatcherConfig(
      handlers: <ExceptionHandler>[
        ConsoleExceptionHandler(includeStackTrace: true),
      ],
      contextProvider: () async => <String, Object?>{
        'env': 'example',
        'appVersion': '1.0.0',
      },
    ),
  );
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Exception Catcher Example',
      theme: ThemeData(useMaterial3: true),
      home: const ExampleHomePage(),
    );
  }
}

class ExampleHomePage extends StatelessWidget {
  const ExampleHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Exception Catcher Example')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            const Text('Tap a button, then check the console output.'),
            const SizedBox(height: 16),
            FilledButton(
              onPressed: _recordHandledException,
              child: const Text('Record handled exception'),
            ),
            const SizedBox(height: 8),
            FilledButton.tonal(
              onPressed: _throwUnhandledAsyncException,
              child: const Text('Throw unhandled async exception'),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _recordHandledException() async {
    ExceptionCatcher.addBreadcrumb(
      'tap record handled exception',
      category: 'user_action',
      data: <String, Object?>{'screen': 'home'},
    );

    try {
      throw StateError('Example handled exception');
    } catch (error, stackTrace) {
      await ExceptionCatcher.recordError(
        error,
        stackTrace,
        customContext: <String, Object?>{'feature': 'manual_record'},
      );
    }
  }

  Future<void> _throwUnhandledAsyncException() async {
    ExceptionCatcher.addBreadcrumb(
      'tap throw unhandled async exception',
      category: 'user_action',
      data: <String, Object?>{'screen': 'home'},
    );

    await Future<void>.delayed(const Duration(milliseconds: 100));
    throw StateError('Example unhandled async exception');
  }
}
0
likes
150
points
29
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter exception collection and reporter.

Repository (GitHub)
View/report issues

Topics

#flutter #exception-handling #crash-reporting

License

MIT (license)

Dependencies

flutter

More

Packages that depend on exception_catcher