1
+ import 'dart:io' ;
2
+
1
3
import 'package:flutter/foundation.dart' ;
2
4
import 'package:flutter/material.dart' ;
3
5
import 'package:instabug_flutter/src/modules/crash_reporting.dart' ;
@@ -7,10 +9,10 @@ class InstabugWidget extends StatefulWidget {
7
9
final Widget child;
8
10
9
11
/// Custom handler for Flutter errors.
10
- ///
12
+ ///
11
13
/// This callback is called when a Flutter error occurs. It receives a
12
14
/// [FlutterErrorDetails] object containing information about the error.
13
- ///
15
+ ///
14
16
/// Example:
15
17
/// ```dart
16
18
/// InstabugWidget(
@@ -21,16 +23,16 @@ class InstabugWidget extends StatefulWidget {
21
23
/// child: MyApp(),
22
24
/// )
23
25
/// ```
24
- ///
26
+ ///
25
27
/// Note: If this handler throws an error, it will be caught and logged
26
28
/// to prevent it from interfering with Instabug's error reporting.
27
29
final Function (FlutterErrorDetails )? flutterErrorHandler;
28
-
30
+
29
31
/// Custom handler for platform errors.
30
- ///
32
+ ///
31
33
/// This callback is called when a platform error occurs. It receives the
32
34
/// error object and stack trace.
33
- ///
35
+ ///
34
36
/// Example:
35
37
/// ```dart
36
38
/// InstabugWidget(
@@ -41,29 +43,41 @@ class InstabugWidget extends StatefulWidget {
41
43
/// child: MyApp(),
42
44
/// )
43
45
/// ```
44
- ///
46
+ ///
45
47
/// Note: If this handler throws an error, it will be caught and logged
46
48
/// to prevent it from interfering with Instabug's error reporting.
47
49
final Function (Object , StackTrace )? platformErrorHandler;
48
50
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
+
49
61
/// This widget is used to wrap the root of your application. It will automatically
50
62
/// configure both FlutterError.onError and PlatformDispatcher.instance.onError handlers to report errors to Instabug.
51
- ///
63
+ ///
52
64
/// Example:
53
- /// ```dart
65
+ /// ```dart
54
66
/// MaterialApp(
55
67
/// home: InstabugWidget(
56
68
/// child: MyApp(),
57
69
/// ),
58
70
/// )
59
71
/// ```
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.
62
74
const InstabugWidget ({
63
75
Key ? key,
64
76
required this .child,
65
77
this .flutterErrorHandler,
66
78
this .platformErrorHandler,
79
+ this .nonFatalFlutterErrors = false ,
80
+ this .shouldExitOnFlutterError = false ,
67
81
}) : super (key: key);
68
82
69
83
@override
@@ -91,14 +105,25 @@ class _InstabugWidgetState extends State<InstabugWidget> {
91
105
}
92
106
}
93
107
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
+ }
98
119
99
120
FlutterError .presentError (details);
100
- };
101
121
122
+ if (widget.shouldExitOnFlutterError) {
123
+ exit (1 );
124
+ }
125
+ };
126
+
102
127
PlatformDispatcher .instance.onError = (Object error, StackTrace stack) {
103
128
// Call user's custom handler if provided
104
129
if (widget.platformErrorHandler != null ) {
0 commit comments