Skip to content
Merged
Prev Previous commit
Next Next commit
add changelog entry
  • Loading branch information
denrase committed Oct 24, 2023
commit 3fb0854adeb370e7915054dfda8156adfe6690eb
39 changes: 38 additions & 1 deletion flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ class MyApp extends StatefulWidget {
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
Widget build(BuildContext context) {
return feedback.BetterFeedback(
Expand All @@ -103,6 +116,30 @@ class _MyAppState extends State<MyApp> {
),
);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
print('App is going into the background');
Future.delayed(const Duration(seconds: 2), () async {
// This code will be executed 2 seconds after the app goes into the background
print('Trigger exception in background');
await _exceptionInBackground();
});
}
}

Future<void> _exceptionInBackground() async {
try {
throw Exception('_exceptionInBackground 2');
} catch (exception, stackTrace) {
await Sentry.captureException(
exception,
stackTrace: stackTrace,
);
}
}
}

class MainScaffold extends StatelessWidget {
Expand Down