main function

Future<void> main()

Implementation

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (useFirebase) {
    try {
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    } catch (e) {
      log(e.toString(), name: 'Firebase Initialization');
    }
  }

  final container = ProviderContainer();

  firebase.log('App starting...');

  void showErrorDialog(Object error, {ErrorType type = ErrorType.unknown}) {
    final context = rootNavigatorKey.currentContext;
    if (context == null) return;
    final errorTitle = switch (type) {
      ErrorType.flutter => t.errors.flutterError,
      ErrorType.async => t.errors.asyncError,
      ErrorType.unknown => t.errors.occurred,
    };

    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(errorTitle),
        // TODO: Remove technical details from user-facing error messages
        content: Text(error.toString()),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: Text(t.general.ok),
          ),
        ],
      ),
    );
  }

  // Pass all uncaught "fatal" errors from the framework to Crashlytics
  FlutterError.onError = (details) {
    firebase.crashlytics?.recordFlutterFatalError(details);
    showErrorDialog(details.exception, type: ErrorType.flutter);
    FlutterError.dumpErrorToConsole(details);
  };

  // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
  PlatformDispatcher.instance.onError = (error, stack) {
    firebase.crashlytics?.recordError(error, stack, fatal: true);
    showErrorDialog(error, type: ErrorType.async);
    log('Uncaught asynchronous error: $error', stackTrace: stack);
    return true;
  };

  firebase.analytics?.logAppOpen();

  await LocaleSettings.useDeviceLocale();

  final authRepository = container.read(authRepositoryProvider);
  final user = await authRepository.getUser();
  final initialLocation = user != null ? AppRoutes.home : AppRoutes.intro;
  final router = createAppRouter(initialLocation: initialLocation);

  runApp(
    UncontrolledProviderScope(
      container: container,
      child: TranslationProvider(
        child: MyApp(router: router),
      ),
    ),
  );
}