Skip to content

Commit

Permalink
Merge pull request #100 from rollbar/fix_310
Browse files Browse the repository at this point in the history
Fix incorrect usage of WidgetFlutterBindings.ensureInitialized()
  • Loading branch information
matux authored May 31, 2023
2 parents 617e182 + dbb1df5 commit 819139a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build_tools/SDK_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.5
3.10.2
2 changes: 1 addition & 1 deletion rollbar_flutter/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand Down
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();
}
}
6 changes: 4 additions & 2 deletions rollbar_flutter/test/rollbar_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ void main() {
when(sender.send(any)).thenAnswer((_) async => true);

callsReceived = [];
channel.setMockMethodCallHandler((methodCall) async {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (methodCall) async {
callsReceived.add(methodCall);
return '42';
});
});

tearDown(() {
debugDefaultTargetPlatformOverride = null;
channel.setMockMethodCallHandler(null);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
});

test('Initialize platform component when running application', () async {
Expand Down

0 comments on commit 819139a

Please sign in to comment.