From fdf8b2c318265074f701a175c55df62e0847584e Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Wed, 2 Aug 2023 11:42:51 -0300 Subject: [PATCH 1/2] Initialize native sdk as a hook --- .../lib/src/hooks/native_hook.dart | 19 +++++++++++++ rollbar_flutter/lib/src/method_channel.dart | 18 +++++++++++++ rollbar_flutter/lib/src/rollbar.dart | 27 +++++++++---------- 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 rollbar_flutter/lib/src/hooks/native_hook.dart create mode 100644 rollbar_flutter/lib/src/method_channel.dart diff --git a/rollbar_flutter/lib/src/hooks/native_hook.dart b/rollbar_flutter/lib/src/hooks/native_hook.dart new file mode 100644 index 0000000..b404b02 --- /dev/null +++ b/rollbar_flutter/lib/src/hooks/native_hook.dart @@ -0,0 +1,19 @@ +import 'package:flutter/services.dart'; +import 'package:rollbar_dart/rollbar.dart'; + +import '../method_channel.dart'; +import 'hook.dart'; + +class NativeHook implements Hook { + static const _platform = MethodChannel('com.rollbar.flutter'); + + @override + Future install(final Config config) async { + await _platform.initialize(config: config); + } + + @override + Future uninstall() async { + await _platform.close(); + } +} diff --git a/rollbar_flutter/lib/src/method_channel.dart b/rollbar_flutter/lib/src/method_channel.dart new file mode 100644 index 0000000..e87334d --- /dev/null +++ b/rollbar_flutter/lib/src/method_channel.dart @@ -0,0 +1,18 @@ +import 'package:flutter/services.dart'; +import 'package:rollbar_flutter/rollbar_flutter.dart'; + +extension RollbarMethodChannel on MethodChannel { + /// The platform-specific path where we can persist data if needed. + Future get persistencePath async => + await invokeMethod('persistencePath'); + + /// Initializes the native Apple/Android SDK Rollbar notifier + /// using the given configuration. + Future initialize({required Config config}) async => + await invokeMethod('initialize', config.toMap()); + + /// Unwinds the native Apple/Android SDK Rollbar notifier. + /// + /// This is a no-op at the moment. + Future close() async => await invokeMethod('close'); +} diff --git a/rollbar_flutter/lib/src/rollbar.dart b/rollbar_flutter/lib/src/rollbar.dart index 625e201..2ae2e45 100644 --- a/rollbar_flutter/lib/src/rollbar.dart +++ b/rollbar_flutter/lib/src/rollbar.dart @@ -10,16 +10,9 @@ import 'package:rollbar_dart/rollbar.dart'; import 'hooks/hook.dart'; import 'hooks/flutter_hook.dart'; import 'hooks/platform_hook.dart'; +import 'hooks/native_hook.dart'; import 'platform_transformer.dart'; - -extension _Methods on MethodChannel { - Future initialize({required Config config}) async => - await invokeMethod('initialize', config.toMap()); - - /// The platform-specific path where we can persist data if needed. - Future get persistencePath async => - await invokeMethod('persistencePath'); -} +import 'method_channel.dart'; typedef RollbarClosure = FutureOr Function(); @@ -35,13 +28,20 @@ class RollbarFlutter { RollbarClosure appRunner, ) async { if (!config.handleUncaughtErrors) { - await _run(config, appRunner); - } else if (requiresCustomZone) { + await _run(config, appRunner, [NativeHook()]); + } else if (!PlatformHook.isAvailable) { await runZonedGuarded( - () async => await _run(config, appRunner, [FlutterHook()]), + () async => await _run(config, appRunner, [ + FlutterHook(), + NativeHook(), + ]), Rollbar.error); } else { - await _run(config, appRunner, [FlutterHook(), PlatformHook()]); + await _run(config, appRunner, [ + FlutterHook(), + PlatformHook(), + NativeHook(), + ]); } } @@ -62,7 +62,6 @@ class RollbarFlutter { await hook.install(config); } - await _platform.initialize(config: config); await appRunner(); } From 416792645143d737c4fba089f6d6bb5e54c2c1c6 Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Wed, 2 Aug 2023 11:43:51 -0300 Subject: [PATCH 2/2] Encapsulate the as dynamic hack inside the platform hook --- rollbar_flutter/lib/src/hooks/platform_hook.dart | 9 +++++++++ rollbar_flutter/lib/src/rollbar.dart | 10 ---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/rollbar_flutter/lib/src/hooks/platform_hook.dart b/rollbar_flutter/lib/src/hooks/platform_hook.dart index 09632eb..e8a7cac 100644 --- a/rollbar_flutter/lib/src/hooks/platform_hook.dart +++ b/rollbar_flutter/lib/src/hooks/platform_hook.dart @@ -6,6 +6,15 @@ class PlatformHook implements Hook { ErrorCallback? _originalOnError; PlatformDispatcher? _platformDispatcher; + static bool get isAvailable { + try { + (PlatformDispatcher.instance as dynamic)?.onError; + return true; + } on NoSuchMethodError { + return false; + } + } + bool onError(Object exception, StackTrace stackTrace) { Rollbar.error(exception, stackTrace); diff --git a/rollbar_flutter/lib/src/rollbar.dart b/rollbar_flutter/lib/src/rollbar.dart index 2ae2e45..ec56b78 100644 --- a/rollbar_flutter/lib/src/rollbar.dart +++ b/rollbar_flutter/lib/src/rollbar.dart @@ -1,6 +1,5 @@ import 'dart:async'; -import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:meta/meta.dart'; @@ -64,13 +63,4 @@ class RollbarFlutter { await appRunner(); } - - static bool get requiresCustomZone { - try { - (PlatformDispatcher.instance as dynamic)?.onError; - return false; - } on NoSuchMethodError { - return true; - } - } }