1+ import 'dart:io' ;
2+
13import 'package:flutter/foundation.dart' ;
24import 'package:flutter/material.dart' ;
35import 'package:instabug_flutter/src/modules/crash_reporting.dart' ;
@@ -7,10 +9,10 @@ class InstabugWidget extends StatefulWidget {
79 final Widget child;
810
911 /// Custom handler for Flutter errors.
10- ///
12+ ///
1113 /// This callback is called when a Flutter error occurs. It receives a
1214 /// [FlutterErrorDetails] object containing information about the error.
13- ///
15+ ///
1416 /// Example:
1517 /// ```dart
1618 /// InstabugWidget(
@@ -21,16 +23,16 @@ class InstabugWidget extends StatefulWidget {
2123 /// child: MyApp(),
2224 /// )
2325 /// ```
24- ///
26+ ///
2527 /// Note: If this handler throws an error, it will be caught and logged
2628 /// to prevent it from interfering with Instabug's error reporting.
2729 final Function (FlutterErrorDetails )? flutterErrorHandler;
28-
30+
2931 /// Custom handler for platform errors.
30- ///
32+ ///
3133 /// This callback is called when a platform error occurs. It receives the
3234 /// error object and stack trace.
33- ///
35+ ///
3436 /// Example:
3537 /// ```dart
3638 /// InstabugWidget(
@@ -41,29 +43,41 @@ class InstabugWidget extends StatefulWidget {
4143 /// child: MyApp(),
4244 /// )
4345 /// ```
44- ///
46+ ///
4547 /// Note: If this handler throws an error, it will be caught and logged
4648 /// to prevent it from interfering with Instabug's error reporting.
4749 final Function (Object , StackTrace )? platformErrorHandler;
4850
51+ /// Whether to handle Flutter errors.
52+ ///
53+ /// If true, the Flutter error will be reported as a non-fatal crash, instead of a fatal crash.
54+ final bool nonFatalFlutterErrors;
55+
56+ /// Whether to exit the app on Flutter error.
57+ ///
58+ /// If true, the app will exit when a Flutter error occurs.
59+ final bool shouldExitOnFlutterError;
60+
4961 /// This widget is used to wrap the root of your application. It will automatically
5062 /// configure both FlutterError.onError and PlatformDispatcher.instance.onError handlers to report errors to Instabug.
51- ///
63+ ///
5264 /// Example:
53- /// ```dart
65+ /// ```dart
5466 /// MaterialApp(
5567 /// home: InstabugWidget(
5668 /// child: MyApp(),
5769 /// ),
5870 /// )
5971 /// ```
60- ///
61- /// Note: Custom error handlers should be provided to handle errors before they are reported to Instabug.
72+ ///
73+ /// Note: Custom error handlers are called before the error is reported to Instabug.
6274 const InstabugWidget ({
6375 Key ? key,
6476 required this .child,
6577 this .flutterErrorHandler,
6678 this .platformErrorHandler,
79+ this .nonFatalFlutterErrors = false ,
80+ this .shouldExitOnFlutterError = false ,
6781 }) : super (key: key);
6882
6983 @override
@@ -91,14 +105,25 @@ class _InstabugWidgetState extends State<InstabugWidget> {
91105 }
92106 }
93107
94- CrashReporting .reportCrash (
95- details.exception,
96- details.stack ?? StackTrace .current,
97- );
108+ if (widget.nonFatalFlutterErrors) {
109+ CrashReporting .reportHandledCrash (
110+ details.exception,
111+ details.stack ?? StackTrace .current,
112+ );
113+ } else {
114+ CrashReporting .reportCrash (
115+ details.exception,
116+ details.stack ?? StackTrace .current,
117+ );
118+ }
98119
99120 FlutterError .presentError (details);
100- };
101121
122+ if (widget.shouldExitOnFlutterError) {
123+ exit (1 );
124+ }
125+ };
126+
102127 PlatformDispatcher .instance.onError = (Object error, StackTrace stack) {
103128 // Call user's custom handler if provided
104129 if (widget.platformErrorHandler != null ) {
0 commit comments