|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:instabug_flutter/src/modules/crash_reporting.dart'; |
| 4 | +import 'package:instabug_flutter/src/utils/instabug_logger.dart'; |
| 5 | + |
| 6 | +class InstabugWidget extends StatefulWidget { |
| 7 | + final Widget child; |
| 8 | + |
| 9 | + /// Custom handler for Flutter errors. |
| 10 | + /// |
| 11 | + /// This callback is called when a Flutter error occurs. It receives a |
| 12 | + /// [FlutterErrorDetails] object containing information about the error. |
| 13 | + /// |
| 14 | + /// Example: |
| 15 | + /// ```dart |
| 16 | + /// InstabugWidget( |
| 17 | + /// flutterErrorHandler: (details) { |
| 18 | + /// print('Flutter error: ${details.exception}'); |
| 19 | + /// // Custom error handling logic |
| 20 | + /// }, |
| 21 | + /// child: MyApp(), |
| 22 | + /// ) |
| 23 | + /// ``` |
| 24 | + /// |
| 25 | + /// Note: If this handler throws an error, it will be caught and logged |
| 26 | + /// to prevent it from interfering with Instabug's error reporting. |
| 27 | + final Function(FlutterErrorDetails)? flutterErrorHandler; |
| 28 | + |
| 29 | + /// Custom handler for platform errors. |
| 30 | + /// |
| 31 | + /// This callback is called when a platform error occurs. It receives the |
| 32 | + /// error object and stack trace. |
| 33 | + /// |
| 34 | + /// Example: |
| 35 | + /// ```dart |
| 36 | + /// InstabugWidget( |
| 37 | + /// platformErrorHandler: (error, stack) { |
| 38 | + /// print('Platform error: $error'); |
| 39 | + /// // Custom error handling logic |
| 40 | + /// }, |
| 41 | + /// child: MyApp(), |
| 42 | + /// ) |
| 43 | + /// ``` |
| 44 | + /// |
| 45 | + /// Note: If this handler throws an error, it will be caught and logged |
| 46 | + /// to prevent it from interfering with Instabug's error reporting. |
| 47 | + final Function(Object, StackTrace)? platformErrorHandler; |
| 48 | + |
| 49 | + /// This widget is used to wrap the root of your application. It will automatically |
| 50 | + /// configure both FlutterError.onError and PlatformDispatcher.instance.onError handlers to report errors to Instabug. |
| 51 | + /// |
| 52 | + /// Example: |
| 53 | + /// ```dart |
| 54 | + /// MaterialApp( |
| 55 | + /// home: InstabugWidget( |
| 56 | + /// child: MyApp(), |
| 57 | + /// ), |
| 58 | + /// ) |
| 59 | + /// ``` |
| 60 | + /// |
| 61 | + /// Note: Custom error handlers should be provided to handle errors before they are reported to Instabug. |
| 62 | + const InstabugWidget({ |
| 63 | + Key? key, |
| 64 | + required this.child, |
| 65 | + this.flutterErrorHandler, |
| 66 | + this.platformErrorHandler, |
| 67 | + }) : super(key: key); |
| 68 | + |
| 69 | + @override |
| 70 | + State<InstabugWidget> createState() => _InstabugWidgetState(); |
| 71 | +} |
| 72 | + |
| 73 | +class _InstabugWidgetState extends State<InstabugWidget> { |
| 74 | + @override |
| 75 | + void initState() { |
| 76 | + super.initState(); |
| 77 | + _setupErrorHandlers(); |
| 78 | + } |
| 79 | + |
| 80 | + void _setupErrorHandlers() { |
| 81 | + FlutterError.onError = (FlutterErrorDetails details) { |
| 82 | + // Call user's custom handler if provided |
| 83 | + if (widget.flutterErrorHandler != null) { |
| 84 | + try { |
| 85 | + widget.flutterErrorHandler!(details); |
| 86 | + } catch (e) { |
| 87 | + InstabugLogger.I.e( |
| 88 | + 'Custom Flutter error handler failed: $e', |
| 89 | + tag: 'InstabugWidget', |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + CrashReporting.reportCrash( |
| 95 | + details.exception, |
| 96 | + details.stack ?? StackTrace.current, |
| 97 | + ); |
| 98 | + |
| 99 | + FlutterError.presentError(details); |
| 100 | + }; |
| 101 | + |
| 102 | + PlatformDispatcher.instance.onError = (Object error, StackTrace stack) { |
| 103 | + // Call user's custom handler if provided |
| 104 | + if (widget.platformErrorHandler != null) { |
| 105 | + try { |
| 106 | + widget.platformErrorHandler!(error, stack); |
| 107 | + } catch (e) { |
| 108 | + InstabugLogger.I.e( |
| 109 | + 'Custom platform error handler failed: $e', |
| 110 | + tag: 'InstabugWidget', |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + CrashReporting.reportCrash(error, stack); |
| 116 | + |
| 117 | + return true; |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + @override |
| 122 | + Widget build(BuildContext context) { |
| 123 | + return widget.child; |
| 124 | + } |
| 125 | +} |
0 commit comments