Skip to content

null safety on sentry_flutter #337

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

Merged
merged 17 commits into from
Mar 7, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Fix: Do not append stack trace to the exception if there are no frames
* Fix: Empty DSN disables the SDK and runs the App
* Refactoring: Migrate Sentry Dart to null safety
* Fix: Null safety on sentry_flutter (#337)

# 4.0.6

Expand Down
38 changes: 16 additions & 22 deletions flutter/lib/src/default_integrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'widgets_binding_observer.dart';
class WidgetsFlutterBindingIntegration
extends Integration<SentryFlutterOptions> {
WidgetsFlutterBindingIntegration(
[WidgetsBinding Function() ensureInitialized])
[WidgetsBinding Function()? ensureInitialized])
: _ensureInitialized =
ensureInitialized ?? WidgetsFlutterBinding.ensureInitialized;

Expand Down Expand Up @@ -104,7 +104,7 @@ class LoadContextsIntegration extends Integration<SentryFlutterOptions> {
(event, {hint}) async {
try {
final infos = Map<String, dynamic>.from(
await _channel.invokeMethod('loadContexts'),
await (_channel.invokeMethod('loadContexts')),
);
if (infos['contexts'] != null) {
final contexts = Contexts.fromJson(
Expand Down Expand Up @@ -137,7 +137,7 @@ class LoadContextsIntegration extends Integration<SentryFlutterOptions> {
if (infos['package'] != null) {
final package = Map<String, String>.from(infos['package'] as Map);
final sdk = event.sdk ?? options.sdk;
sdk.addPackage(package['sdk_name'], package['version']);
sdk.addPackage(package['sdk_name']!, package['version']!);
event = event.copyWith(sdk: sdk);
}
} catch (error) {
Expand Down Expand Up @@ -201,7 +201,7 @@ class NativeSdkIntegration extends Integration<SentryFlutterOptions> {
/// - [SentryWidgetsBindingObserver]
/// - [WidgetsBindingObserver](https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html)
class WidgetsBindingIntegration extends Integration<SentryFlutterOptions> {
SentryWidgetsBindingObserver _observer;
SentryWidgetsBindingObserver? _observer;

@override
FutureOr<void> call(Hub hub, SentryFlutterOptions options) {
Expand All @@ -215,7 +215,7 @@ class WidgetsBindingIntegration extends Integration<SentryFlutterOptions> {
// If the instance is not created, we skip it to keep going.
final instance = WidgetsBinding.instance;
if (instance != null) {
instance.addObserver(_observer);
instance.addObserver(_observer!);
options.sdk.addIntegration('widgetsBindingIntegration');
} else {
options.logger(
Expand All @@ -228,8 +228,8 @@ class WidgetsBindingIntegration extends Integration<SentryFlutterOptions> {
@override
void close() {
final instance = WidgetsBinding.instance;
if (instance != null) {
instance.removeObserver(_observer);
if (instance != null && _observer != null) {
instance.removeObserver(_observer!);
}
}
}
Expand All @@ -246,10 +246,8 @@ class LoadAndroidImageListIntegration
options.addEventProcessor(
(event, {hint}) async {
try {
if (event.exception != null &&
event.exception.stackTrace != null &&
event.exception.stackTrace.frames != null) {
final needsSymbolication = event.exception.stackTrace.frames
if (event.exception != null && event.exception!.stackTrace != null) {
final needsSymbolication = event.exception!.stackTrace!.frames
.any((element) => 'native' == element.platform);

// if there are no frames that require symbolication, we don't
Expand All @@ -264,7 +262,7 @@ class LoadAndroidImageListIntegration
// we call on every event because the loaded image list is cached
// and it could be changed on the Native side.
final imageList = List<Map<dynamic, dynamic>>.from(
await _channel.invokeMethod('loadImageList'),
await (_channel.invokeMethod('loadImageList')),
);

if (imageList.isEmpty) {
Expand All @@ -274,13 +272,13 @@ class LoadAndroidImageListIntegration
final newDebugImages = <DebugImage>[];

for (final item in imageList) {
final codeFile = item['code_file'] as String;
final codeId = item['code_id'] as String;
final imageAddr = item['image_addr'] as String;
final imageSize = item['image_size'] as int;
final codeFile = item['code_file'] as String?;
final codeId = item['code_id'] as String?;
final imageAddr = item['image_addr'] as String?;
final imageSize = item['image_size'] as int?;
final type = item['type'] as String;
final debugId = item['debug_id'] as String;
final debugFile = item['debug_file'] as String;
final debugId = item['debug_id'] as String?;
final debugFile = item['debug_file'] as String?;

final image = DebugImage(
type: type,
Expand Down Expand Up @@ -326,10 +324,6 @@ class LoadReleaseIntegration extends Integration<SentryFlutterOptions> {
FutureOr<void> call(Hub hub, SentryFlutterOptions options) async {
try {
if (!kIsWeb) {
if (_packageLoader == null) {
options.logger(SentryLevel.debug, 'Package loader is null.');
return;
}
final packageInfo = await _packageLoader();
final release =
'${packageInfo.packageName}@${packageInfo.version}+${packageInfo.buildNumber}';
Expand Down
45 changes: 20 additions & 25 deletions flutter/lib/src/navigation/sentry_navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,22 @@ const _navigationKey = 'navigation';
/// - [RouteObserver](https://api.flutter.dev/flutter/widgets/RouteObserver-class.html)
/// - [Navigating with arguments](https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments)
class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
factory SentryNavigatorObserver({Hub hub}) {
return SentryNavigatorObserver._(hub ?? HubAdapter());
}

SentryNavigatorObserver._(this.hub) : assert(hub != null);
SentryNavigatorObserver({Hub? hub}) : hub = hub ?? HubAdapter();

final Hub hub;

@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
_addBreadcrumb(
type: 'didPush',
from: previousRoute?.settings,
to: route?.settings,
to: route.settings,
);
}

@override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);

_addBreadcrumb(
Expand All @@ -65,20 +61,20 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
}

@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);

_addBreadcrumb(
type: 'didPop',
from: route?.settings,
from: route.settings,
to: previousRoute?.settings,
);
}

void _addBreadcrumb({
String type,
RouteSettings from,
RouteSettings to,
required String type,
RouteSettings? from,
RouteSettings? to,
}) {
hub.addBreadcrumb(RouteObserverBreadcrumb(
navigationType: type,
Expand All @@ -98,10 +94,10 @@ class RouteObserverBreadcrumb extends Breadcrumb {
factory RouteObserverBreadcrumb({
/// This should correspond to Flutters navigation events.
/// See https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
@required String navigationType,
RouteSettings from,
RouteSettings to,
SentryLevel level,
required String navigationType,
RouteSettings? from,
RouteSettings? to,
SentryLevel? level,
}) {
final dynamic fromArgs = _formatArgs(from?.arguments);
final dynamic toArgs = _formatArgs(to?.arguments);
Expand All @@ -116,26 +112,25 @@ class RouteObserverBreadcrumb extends Breadcrumb {
}

RouteObserverBreadcrumb._({
@required String navigationType,
String from,
required String navigationType,
String? from,
dynamic fromArgs,
String to,
String? to,
dynamic toArgs,
SentryLevel level,
}) : assert(navigationType != null),
super(
SentryLevel? level,
}) : super(
category: _navigationKey,
type: _navigationKey,
level: level,
data: <String, dynamic>{
if (navigationType != null) 'state': navigationType,
'state': navigationType,
if (from != null) 'from': from,
if (fromArgs != null) 'from_arguments': fromArgs,
if (to != null) 'to': to,
if (toArgs != null) 'to_arguments': toArgs,
});

static dynamic _formatArgs(Object args) {
static dynamic _formatArgs(Object? args) {
if (args == null) {
return null;
}
Expand Down
8 changes: 2 additions & 6 deletions flutter/lib/src/sentry_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ mixin SentryFlutter {

static Future<void> init(
FlutterOptionsConfiguration optionsConfiguration, {
AppRunner appRunner,
AppRunner? appRunner,
PackageLoader packageLoader = _loadPackageInfo,
iOSPlatformChecker isIOSChecker = isIOS,
AndroidPlatformChecker isAndroidChecker = isAndroid,
MethodChannel channel = _channel,
}) async {
if (optionsConfiguration == null) {
throw ArgumentError('OptionsConfiguration is required.');
}

final flutterOptions = SentryFlutterOptions();

// first step is to install the native integration and set default values,
Expand All @@ -53,7 +49,7 @@ mixin SentryFlutter {

await Sentry.init(
(options) async {
await optionsConfiguration(options);
await optionsConfiguration(options as SentryFlutterOptions);
},
appRunner: appRunner,
options: flutterOptions,
Expand Down
Loading