-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.dart
More file actions
120 lines (102 loc) · 3.58 KB
/
main.dart
File metadata and controls
120 lines (102 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'dart:developer';
import 'dart:ui';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:tattoo/firebase_options.dart';
import 'package:tattoo/i18n/strings.g.dart';
import 'package:tattoo/repositories/auth_repository.dart';
import 'package:tattoo/router/app_router.dart';
import 'package:tattoo/services/firebase_service.dart';
enum ErrorType {
flutter,
async,
unknown,
}
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();
final firebase = container.read(firebaseServiceProvider);
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,
};
WidgetsBinding.instance.addPostFrameCallback((_) {
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(firebase, initialLocation: initialLocation);
runApp(
UncontrolledProviderScope(
container: container,
child: TranslationProvider(
child: MyApp(router: router),
),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key, required this.router});
final GoRouter router;
static const themeColor = Color(0xFF4B709B);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: t.general.appTitle,
locale: TranslationProvider.of(context).flutterLocale,
supportedLocales: AppLocaleUtils.supportedLocales,
localizationsDelegates: GlobalMaterialLocalizations.delegates,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: themeColor),
),
routerConfig: router,
);
}
}