Skip to content

Commit

Permalink
Ensure WidgetFlutterBinding.ensureInitialized() is run on the same zone
Browse files Browse the repository at this point in the history
  • Loading branch information
matux committed May 31, 2023
1 parent 90eff1f commit dbb1df5
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions rollbar_flutter/lib/src/rollbar.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<void> _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();
}
}

0 comments on commit dbb1df5

Please sign in to comment.