Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect usage of WidgetFlutterBindings.ensureInitialized() #100

Merged
merged 4 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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