initFimberInMain static method

Future<void> initFimberInMain(
  1. FirebaseOptions? firebaseOptions
)

Implementation

static Future<void> initFimberInMain(FirebaseOptions? firebaseOptions) async {
  if (_isInitializedInMain) return;
  _isInitializedInMain = true;

  FutureBuilder.debugRethrowError = true;

  WidgetsFlutterBinding.ensureInitialized();

  if (kDebugMode) {
    Fimber.plantTree(DSDebugTree(useColors: true));
  }
  Fimber.plantTree(DSCrashReportingTree());

  FlutterError.onError = (details) {
    Fimber.e('${details.exception}', stacktrace: details.stack);
  };
  PlatformDispatcher.instance.onError = (error, stack) {
    Fimber.e('$error', stacktrace: stack);
    return true;
  };

  try {
    await Firebase.initializeApp(options: firebaseOptions);
  } catch (e, trace) {
    Fimber.e('$e', stacktrace: trace);
  }

  if (!kIsWeb) {
    Isolate.current.addErrorListener(RawReceivePort((pair) async {
      final List<dynamic> errorAndStacktrace = pair;
      final error = errorAndStacktrace.first;
      final stackText = errorAndStacktrace.last as String?;
      final StackTrace? stack;
      if (stackText?.isNotEmpty == true) {
        stack = StackTrace.fromString(stackText!);
      } else {
        stack = null;
      }
      Fimber.e('$error', stacktrace: stack);
    }).sendPort);

    if (kDebugMode) {
      await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false);
    }
    unawaited(FirebaseCrashlytics.instance.setUserIdentifier(DSMetrica.yandexId));

    IsolateNameServer.removePortNameMapping(_portName);
    final port = ReceivePort(_portName);
    final res = IsolateNameServer.registerPortWithName(port.sendPort, _portName);
    if (!res) {
      Fimber.e('Failed to register port $_portName', stacktrace: StackTrace.current);
    }
    port.listen((message) {
      final level = message[0] as String;
      final msg = message[1] as String;
      final tag = message[2] as String?;
      final ex = message[3];
      final stack = message[4] is String ? StackTrace.fromString(message[4]) : null;
      final attrsJson = message[5] as String?;
      final attrs = attrsJson != null ? jsonDecode(attrsJson) : null;
      Fimber.log(level, msg, tag: tag, ex: ex, stacktrace: stack, attributes: attrs);
    });
  }
}