11import 'dart:async' ;
22
3+ import 'package:flutter/foundation.dart' ;
34import 'package:flutter/material.dart' ;
45import 'package:posthog_flutter/posthog_flutter.dart' ;
6+ import 'package:posthog_flutter_example/error_example.dart' ;
57
68Future <void > main () async {
7- // // init WidgetsFlutterBinding if not yet
8-
9- WidgetsFlutterBinding .ensureInitialized ();
109 final config =
1110 PostHogConfig ('phc_6lqCaCDCBEWdIGieihq5R2dZpPVbAUFISA75vFZow06' );
1211 config.onFeatureFlags = () {
@@ -15,8 +14,8 @@ Future<void> main() async {
1514 config.debug = true ;
1615 config.captureApplicationLifecycleEvents = false ;
1716 config.host = 'https://us.i.posthog.com' ;
18- config.surveys = true ;
19- config.sessionReplay = true ;
17+ config.surveys = false ;
18+ config.sessionReplay = false ;
2019 config.sessionReplayConfig.maskAllTexts = false ;
2120 config.sessionReplayConfig.maskAllImages = false ;
2221 config.sessionReplayConfig.throttleDelay = const Duration (milliseconds: 1000 );
@@ -30,8 +29,20 @@ Future<void> main() async {
3029 config.errorTrackingConfig.captureIsolateErrors =
3130 true ; // Capture isolate errors
3231
33- await Posthog ().setup (config);
32+ if (kIsWeb) {
33+ runZonedGuarded (
34+ () async => await _initAndRun (config),
35+ (error, stackTrace) async => await Posthog ()
36+ .captureRunZonedGuardedError (error: error, stackTrace: stackTrace),
37+ );
38+ } else {
39+ await _initAndRun (config);
40+ }
41+ }
3442
43+ Future <void > _initAndRun (PostHogConfig config) async {
44+ WidgetsFlutterBinding .ensureInitialized ();
45+ await Posthog ().setup (config);
3546 runApp (const MyApp ());
3647}
3748
@@ -267,56 +278,10 @@ class InitialScreenState extends State<InitialScreen> {
267278 ),
268279 ElevatedButton (
269280 onPressed: () async {
270- try {
271- // Simulate an exception in main isolate
272- // throw 'a custom error string';
273- // throw 333;
274- throw CustomException (
275- 'This is a custom exception with additional context' ,
276- code: 'DEMO_ERROR_001' ,
277- additionalData: {
278- 'user_action' : 'button_press' ,
279- 'timestamp' : DateTime .now ().millisecondsSinceEpoch,
280- 'feature_enabled' : true ,
281- },
282- );
283- } catch (e, stack) {
284- await Posthog ().captureException (
285- error: e,
286- stackTrace: stack,
287- properties: {
288- 'test_type' : 'main_isolate_exception' ,
289- 'button_pressed' : 'capture_exception_main' ,
290- 'exception_category' : 'custom' ,
291- },
292- );
293-
294- if (mounted && context.mounted) {
295- ScaffoldMessenger .of (context).showSnackBar (
296- const SnackBar (
297- content: Text (
298- 'Main isolate exception captured successfully! Check PostHog.' ),
299- backgroundColor: Colors .green,
300- duration: Duration (seconds: 3 ),
301- ),
302- );
303- }
304- }
281+ await ErrorExample ().causeHandledDivisionError ();
305282 },
306283 child: const Text ("Capture Exception" ),
307284 ),
308- ElevatedButton (
309- style: ElevatedButton .styleFrom (
310- backgroundColor: Colors .orange,
311- ),
312- onPressed: () async {
313- await Posthog ().captureException (
314- error: 'No Stack Trace Error' ,
315- properties: {'test_type' : 'no_stack_trace' },
316- );
317- },
318- child: const Text ("Capture Exception (Missing Stack)" ),
319- ),
320285 const Divider (),
321286 const Padding (
322287 padding: EdgeInsets .all (8.0 ),
@@ -330,7 +295,7 @@ class InitialScreenState extends State<InitialScreen> {
330295 backgroundColor: Colors .red,
331296 foregroundColor: Colors .white,
332297 ),
333- onPressed: () {
298+ onPressed: () async {
334299 if (mounted) {
335300 ScaffoldMessenger .of (context).showSnackBar (
336301 const SnackBar (
@@ -343,10 +308,7 @@ class InitialScreenState extends State<InitialScreen> {
343308 }
344309
345310 // Test Flutter error handler by throwing in widget context
346- throw const CustomException (
347- 'Test Flutter error for autocapture' ,
348- code: 'FlutterErrorTest' ,
349- additionalData: {'test_type' : 'flutter_error' });
311+ await ErrorExample ().causeHandledDivisionError ();
350312 },
351313 child: const Text ("Test Flutter Error Handler" ),
352314 ),
@@ -355,18 +317,10 @@ class InitialScreenState extends State<InitialScreen> {
355317 backgroundColor: Colors .blue,
356318 foregroundColor: Colors .white,
357319 ),
358- onPressed: () {
359- // Test PlatformDispatcher error handler with Future
360- Future .delayed (Duration .zero, () {
361- throw const CustomException (
362- 'Test PlatformDispatcher error for autocapture' ,
363- code: 'PlatformDispatcherTest' ,
364- additionalData: {
365- 'test_type' : 'platform_dispatcher_error'
366- });
367- });
320+ onPressed: () async {
321+ await ErrorExample ().throwWithinDelayed ();
368322
369- if (mounted) {
323+ if (mounted && context.mounted ) {
370324 ScaffoldMessenger .of (context).showSnackBar (
371325 const SnackBar (
372326 content: Text (
@@ -384,19 +338,11 @@ class InitialScreenState extends State<InitialScreen> {
384338 backgroundColor: Colors .purple,
385339 foregroundColor: Colors .white,
386340 ),
387- onPressed: () {
341+ onPressed: () async {
388342 // Test isolate error listener by throwing in an async callback
389- Timer (Duration .zero, () {
390- throw const CustomException (
391- 'Isolate error for testing' ,
392- code: 'IsolateHandlerTest' ,
393- additionalData: {
394- 'test_type' : 'isolate_error_listener_timer' ,
395- },
396- );
397- });
343+ await ErrorExample ().throwWithinTimer ();
398344
399- if (mounted) {
345+ if (mounted && context.mounted ) {
400346 ScaffoldMessenger .of (context).showSnackBar (
401347 const SnackBar (
402348 content:
0 commit comments