From dbb1df566befade921f0bd49afdf6f27495a41da Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Wed, 31 May 2023 17:55:39 -0300 Subject: [PATCH] Ensure WidgetFlutterBinding.ensureInitialized() is run on the same zone --- rollbar_flutter/lib/src/rollbar.dart | 37 +++++++++++++++++----------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/rollbar_flutter/lib/src/rollbar.dart b/rollbar_flutter/lib/src/rollbar.dart index ba76193..35c4482 100644 --- a/rollbar_flutter/lib/src/rollbar.dart +++ b/rollbar_flutter/lib/src/rollbar.dart @@ -1,8 +1,9 @@ import 'dart:async'; -import 'package:meta/meta.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; +import 'package:meta/meta.dart'; import 'package:rollbar_dart/rollbar.dart'; @@ -30,29 +31,37 @@ class RollbarFlutter { Config config, RollbarClosure appRunner, ) async { - WidgetsFlutterBinding.ensureInitialized(); + if (!config.handleUncaughtErrors) { + WidgetsFlutterBinding.ensureInitialized(); - await Rollbar.run(config.copyWith( - framework: 'flutter', - persistencePath: await _platform.persistencePath, - transformer: (_) => PlatformTransformer(), - )); + await _run(config, appRunner, null); - if (!config.handleUncaughtErrors) { - await _platform.initialize(config: config); - await appRunner(); return; } await runZonedGuarded(() async { WidgetsFlutterBinding.ensureInitialized(); - FlutterError.onError = RollbarFlutterError.onError; - - await _platform.initialize(config: config); - await appRunner(); + await _run(config, appRunner, RollbarFlutterError.onError); }, (exception, stackTrace) { Rollbar.error(exception, stackTrace); }); } + + static Future _run( + Config config, + RollbarClosure appRunner, + FlutterExceptionHandler? onError, + ) async { + await Rollbar.run(config.copyWith( + framework: 'flutter', + persistencePath: await _platform.persistencePath, + transformer: (_) => PlatformTransformer(), + )); + + FlutterError.onError ??= onError; + + await _platform.initialize(config: config); + await appRunner(); + } }