Skip to content

Commit

Permalink
refactor: update to implements InitializableDependency (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrarafer authored Jun 27, 2024
1 parent 06a7f66 commit 7b33424
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
56 changes: 24 additions & 32 deletions lib/src/crashlytics_service.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:stacked/stacked_annotations.dart';

class CrashlyticsService {
static CrashlyticsService? _instance;
class CrashlyticsService implements InitializableDependency {
late FirebaseCrashlytics _instance;

static Future<CrashlyticsService> getInstance() async {
_instance ??= CrashlyticsService._(FirebaseCrashlytics.instance);

return _instance!;
@override
Future<void> init() async {
_instance = FirebaseCrashlytics.instance;
}

final FirebaseCrashlytics _crashlyticsService;
CrashlyticsService._(
this._crashlyticsService,
);

void recordFlutterErrorToCrashlytics(FlutterErrorDetails details) {
try {
_crashlyticsService.recordFlutterError(details);
_instance.recordFlutterError(details);
} catch (e) {
_catchOrThrow(e);
}
}

Future setUserIdToCrashlytics({String? id}) async {
try {
if (id != null) await _crashlyticsService.setUserIdentifier(id);
if (id != null) await _instance.setUserIdentifier(id);
} catch (e) {
_catchOrThrow(e);
}
Expand All @@ -39,26 +34,23 @@ class CrashlyticsService {
required bool logwarnings,
}) async {
try {
if (level == Level.error || level == Level.wtf || level == Level.fatal) {
await _crashlyticsService.recordError(
if (level == Level.error || level == Level.fatal) {
await _instance.recordError(
lines.join('\n'),
stacktrace,
printDetails: true,
fatal: true,
);
}
if (level == Level.warning && logwarnings) {
await _crashlyticsService.recordError(
await _instance.recordError(
lines.join('\n'),
stacktrace,
printDetails: true,
);
}
if (level == Level.info ||
level == Level.verbose ||
level == Level.trace ||
level == Level.debug) {
await _crashlyticsService.log(lines.join('\n'));
if (level == Level.info || level == Level.trace || level == Level.debug) {
await _instance.log(lines.join('\n'));
}
} catch (exception) {
_catchOrThrow(exception);
Expand All @@ -67,7 +59,7 @@ class CrashlyticsService {

Future setCustomKeysToTrack(String key, dynamic value) async {
try {
await _crashlyticsService.setCustomKey(key, value);
await _instance.setCustomKey(key, value);
} catch (e) {
_catchOrThrow(e);
}
Expand All @@ -77,7 +69,7 @@ class CrashlyticsService {
// So, be sure to remove it after usage
void crashApp() {
try {
_crashlyticsService.crash();
_instance.crash();
} catch (e) {
_catchOrThrow(e);
}
Expand All @@ -99,16 +91,16 @@ class CrashlyticsOutput extends LogOutput {
CrashlyticsOutput({this.logWarnings = false});

@override
void output(OutputEvent event) {
Future<void> output(OutputEvent event) async {
try {
CrashlyticsService.getInstance().then((instance) {
return instance.logToCrashlytics(
event.level,
event.lines,
StackTrace.current,
logwarnings: logWarnings,
);
});
final service = CrashlyticsService();
await service.init();
return service.logToCrashlytics(
event.level,
event.lines,
StackTrace.current,
logwarnings: logWarnings,
);
} catch (e) {
if (kDebugMode) {
print('CRASHLYTICS FAILED: $e');
Expand Down
1 change: 1 addition & 0 deletions lib/stacked_crashlytics.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
library stacked_crashlytics;

export 'src/crashlytics_service.dart';
export 'package:logger/logger.dart' show Level;
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
firebase_core: ^2.32.0
firebase_crashlytics: ^3.5.7
logger: ^2.3.0
stacked: ^3.4.3

dev_dependencies:
firebase_core_platform_interface: ^5.0.0
Expand Down
4 changes: 3 additions & 1 deletion test/stacked_crashlytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ void main() {
setupFirebaseAuthMocks();
Firebase.initializeApp();
});

test('when called, it should crash the app', () async {
var service = await CrashlyticsService.getInstance();
final service = CrashlyticsService();
await service.init();
service.crashApp();
});
});
Expand Down

0 comments on commit 7b33424

Please sign in to comment.