1+ import 'dart:async' ;
2+
13import 'package:flutter/material.dart' ;
24import 'package:posthog_flutter/posthog_flutter.dart' ;
35
@@ -16,6 +18,15 @@ Future<void> main() async {
1618 config.sessionReplayConfig.maskAllImages = false ;
1719 config.sessionReplayConfig.throttleDelay = const Duration (milliseconds: 1000 );
1820 config.flushAt = 1 ;
21+
22+ // Configure error tracking and exception capture
23+ config.errorTrackingConfig.captureFlutterErrors =
24+ true ; // Capture Flutter framework errors
25+ config.errorTrackingConfig.capturePlatformDispatcherErrors =
26+ true ; // Capture Dart runtime errors
27+ config.errorTrackingConfig.captureIsolateErrors =
28+ true ; // Capture isolate errors
29+
1930 await Posthog ().setup (config);
2031
2132 runApp (const MyApp ());
@@ -243,7 +254,7 @@ class InitialScreenState extends State<InitialScreen> {
243254 const Padding (
244255 padding: EdgeInsets .all (8.0 ),
245256 child: Text (
246- "Error Tracking" ,
257+ "Error Tracking - Manual " ,
247258 style: TextStyle (fontWeight: FontWeight .bold),
248259 ),
249260 ),
@@ -300,6 +311,98 @@ class InitialScreenState extends State<InitialScreen> {
300311 child: const Text ("Capture Exception (Missing Stack)" ),
301312 ),
302313 const Divider (),
314+ const Padding (
315+ padding: EdgeInsets .all (8.0 ),
316+ child: Text (
317+ "Error Tracking - Autocapture" ,
318+ style: TextStyle (fontWeight: FontWeight .bold),
319+ ),
320+ ),
321+ ElevatedButton (
322+ style: ElevatedButton .styleFrom (
323+ backgroundColor: Colors .red,
324+ foregroundColor: Colors .white,
325+ ),
326+ onPressed: () {
327+ if (mounted) {
328+ ScaffoldMessenger .of (context).showSnackBar (
329+ const SnackBar (
330+ content:
331+ Text ('Flutter error triggered! Check PostHog.' ),
332+ backgroundColor: Colors .red,
333+ duration: Duration (seconds: 3 ),
334+ ),
335+ );
336+ }
337+
338+ // Test Flutter error handler by throwing in widget context
339+ throw const CustomException (
340+ 'Test Flutter error for autocapture' ,
341+ code: 'FlutterErrorTest' ,
342+ additionalData: {'test_type' : 'flutter_error' });
343+ },
344+ child: const Text ("Test Flutter Error Handler" ),
345+ ),
346+ ElevatedButton (
347+ style: ElevatedButton .styleFrom (
348+ backgroundColor: Colors .blue,
349+ foregroundColor: Colors .white,
350+ ),
351+ onPressed: () {
352+ // Test PlatformDispatcher error handler with Future
353+ Future .delayed (Duration .zero, () {
354+ throw const CustomException (
355+ 'Test PlatformDispatcher error for autocapture' ,
356+ code: 'PlatformDispatcherTest' ,
357+ additionalData: {
358+ 'test_type' : 'platform_dispatcher_error'
359+ });
360+ });
361+
362+ if (mounted) {
363+ ScaffoldMessenger .of (context).showSnackBar (
364+ const SnackBar (
365+ content: Text (
366+ 'Dart runtime error triggered! Check PostHog.' ),
367+ backgroundColor: Colors .blue,
368+ duration: Duration (seconds: 3 ),
369+ ),
370+ );
371+ }
372+ },
373+ child: const Text ("Test Dart Error Handler" ),
374+ ),
375+ ElevatedButton (
376+ style: ElevatedButton .styleFrom (
377+ backgroundColor: Colors .purple,
378+ foregroundColor: Colors .white,
379+ ),
380+ onPressed: () {
381+ // Test isolate error listener by throwing in an async callback
382+ Timer (Duration .zero, () {
383+ throw const CustomException (
384+ 'Isolate error for testing' ,
385+ code: 'IsolateHandlerTest' ,
386+ additionalData: {
387+ 'test_type' : 'isolate_error_listener_timer' ,
388+ },
389+ );
390+ });
391+
392+ if (mounted) {
393+ ScaffoldMessenger .of (context).showSnackBar (
394+ const SnackBar (
395+ content:
396+ Text ('Isolate error triggered! Check PostHog.' ),
397+ backgroundColor: Colors .purple,
398+ duration: Duration (seconds: 3 ),
399+ ),
400+ );
401+ }
402+ },
403+ child: const Text ("Test Isolate Error Handler" ),
404+ ),
405+ const Divider (),
303406 const Padding (
304407 padding: EdgeInsets .all (8.0 ),
305408 child: Text (
0 commit comments