main.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import 'package:provider/provider.dart';
  2. import 'package:flutter/gestures.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_localizations/flutter_localizations.dart';
  5. import 'package:flutter_web_plugins/url_strategy.dart';
  6. import 'auth/custom_auth/auth_util.dart';
  7. import 'auth/custom_auth/custom_auth_user_provider.dart';
  8. import 'backend/firebase/firebase_config.dart';
  9. import '/flutter_flow/flutter_flow_theme.dart';
  10. import 'flutter_flow/flutter_flow_util.dart';
  11. import 'flutter_flow/internationalization.dart';
  12. import 'package:flutter_spinkit/flutter_spinkit.dart';
  13. import 'package:flutter/foundation.dart';
  14. import 'package:firebase_crashlytics/firebase_crashlytics.dart';
  15. import 'flutter_flow/nav/nav.dart';
  16. import 'index.dart';
  17. import 'dart:async';
  18. import 'package:easy_debounce/easy_debounce.dart';
  19. void main() async {
  20. WidgetsFlutterBinding.ensureInitialized();
  21. GoRouter.optionURLReflectsImperativeAPIs = true;
  22. usePathUrlStrategy();
  23. await initFirebase();
  24. await authManager.initialize();
  25. final appState = FFAppState(); // Initialize FFAppState
  26. await appState.initializePersistedState();
  27. debugLogAppState(appState);
  28. appState.addListener(() {
  29. debugLogAppState(appState);
  30. });
  31. if (!kIsWeb) {
  32. FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
  33. }
  34. final originalErrorWidgetBuilder = ErrorWidget.builder;
  35. ErrorWidget.builder = (FlutterErrorDetails details) {
  36. try {
  37. final match = RegExp(
  38. r'The relevant error-causing widget was:\s+([a-zA-Z0-9]+)(.|\n)*When the exception was thrown, this was the stack:((.|\n)*)')
  39. .firstMatch(details.toString());
  40. if (match == null) {
  41. return originalErrorWidgetBuilder(details);
  42. }
  43. final widgetName = match.group(1);
  44. final stackTrace = match.group(3)!;
  45. // The stack trace usually is very long, and most of it is entirely
  46. // irrelevant for troubleshooting, e.g.:
  47. //
  48. // dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 251:49 throw_
  49. // dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3 assertFailed
  50. // packages/flutter/src/widgets/text.dart 378:14 new
  51. // packages/debug_screen_test/home_page/home_page_widget.dart 51:15 build
  52. // packages/flutter/src/widgets/framework.dart 4870:27 build
  53. // packages/flutter/src/widgets/framework.dart 4754:15 performRebuild
  54. // packages/flutter/src/widgets/framework.dart 4928:11 performRebuild
  55. // packages/flutter/src/widgets/framework.dart 4477:5 rebuild
  56. // <a long long list of internal libraries>
  57. //
  58. // We truncate everything after project-specific code.
  59. final filteredStackTrace = <String>[];
  60. var foundProjectTraces = false;
  61. for (final line in stackTrace.split('\n')) {
  62. if (line.startsWith('packages/uitgaanskrant/')) {
  63. foundProjectTraces = true;
  64. } else {
  65. if (foundProjectTraces) {
  66. filteredStackTrace.add('...');
  67. break;
  68. }
  69. }
  70. filteredStackTrace.add(line);
  71. }
  72. final result = '''${details.exceptionAsString()}
  73. The relevant error-causing widget was: $widgetName
  74. Stack trace: ${filteredStackTrace.join("\n")}''';
  75. return ErrorWidget.withDetails(message: result);
  76. } catch (_) {
  77. return originalErrorWidgetBuilder(details);
  78. }
  79. };
  80. /// Every second, fire logging call for different channel (tag) so that frequent
  81. /// logging calls don't get delayed too much
  82. Timer.periodic(const Duration(seconds: 2), (timer) {
  83. EasyDebounce.fire('405ebf2ff50c295c675b5802889ea941f081fd51');
  84. EasyDebounce.cancel('405ebf2ff50c295c675b5802889ea941f081fd51');
  85. EasyDebounce.fire('fbcc19a787981a30d86b10103c2f3951604b2ae6');
  86. EasyDebounce.cancel('fbcc19a787981a30d86b10103c2f3951604b2ae6');
  87. EasyDebounce.fire('c0186d2c21d5d9300ee148206df9fbd1850b8d41');
  88. EasyDebounce.cancel('c0186d2c21d5d9300ee148206df9fbd1850b8d41');
  89. EasyDebounce.fire('508f3c74205c87928b71f49040062e732f9c20b0');
  90. EasyDebounce.cancel('508f3c74205c87928b71f49040062e732f9c20b0');
  91. });
  92. runApp(ChangeNotifierProvider(
  93. create: (context) => appState,
  94. child: MyApp(),
  95. ));
  96. }
  97. class MyApp extends StatefulWidget {
  98. // This widget is the root of your application.
  99. @override
  100. State<MyApp> createState() => _MyAppState();
  101. static _MyAppState of(BuildContext context) =>
  102. context.findAncestorStateOfType<_MyAppState>()!;
  103. }
  104. class _MyAppState extends State<MyApp> {
  105. Locale? _locale;
  106. Locale? get locale => _locale;
  107. ThemeMode _themeMode = ThemeMode.system;
  108. late AppStateNotifier _appStateNotifier;
  109. late GoRouter _router;
  110. String getRoute([RouteMatch? routeMatch]) {
  111. final RouteMatch lastMatch =
  112. routeMatch ?? _router.routerDelegate.currentConfiguration.last;
  113. final RouteMatchList matchList = lastMatch is ImperativeRouteMatch
  114. ? lastMatch.matches
  115. : _router.routerDelegate.currentConfiguration;
  116. return matchList.uri.path;
  117. }
  118. List<String> getRouteStack() =>
  119. _router.routerDelegate.currentConfiguration.matches
  120. .map((e) => getRoute(e))
  121. .toList();
  122. late Stream<UitgaanskrantAuthUser> userStream;
  123. @override
  124. void initState() {
  125. super.initState();
  126. _appStateNotifier = AppStateNotifier.instance;
  127. _router = createRouter(_appStateNotifier);
  128. userStream = uitgaanskrantAuthUserStream()
  129. ..listen((user) {
  130. _appStateNotifier.update(user);
  131. debugLogAuthenticatedUser();
  132. });
  133. Future.delayed(
  134. Duration(milliseconds: 1000),
  135. () => _appStateNotifier.stopShowingSplashImage(),
  136. );
  137. _router.routerDelegate.addListener(() {
  138. if (mounted) {
  139. debugLogGlobalProperty(
  140. context,
  141. locale: locale.toString(),
  142. routePath: getRoute(),
  143. routeStack: getRouteStack(),
  144. );
  145. }
  146. });
  147. }
  148. void setLocale(String language) {
  149. safeSetState(() => _locale = createLocale(language));
  150. }
  151. void setThemeMode(ThemeMode mode) => safeSetState(() {
  152. _themeMode = mode;
  153. });
  154. @override
  155. Widget build(BuildContext context) {
  156. return MaterialApp.router(
  157. debugShowCheckedModeBanner: false,
  158. title: 'Uitgaanskrant',
  159. localizationsDelegates: [
  160. FFLocalizationsDelegate(),
  161. GlobalMaterialLocalizations.delegate,
  162. GlobalWidgetsLocalizations.delegate,
  163. GlobalCupertinoLocalizations.delegate,
  164. FallbackMaterialLocalizationDelegate(),
  165. FallbackCupertinoLocalizationDelegate(),
  166. ],
  167. locale: _locale,
  168. supportedLocales: const [
  169. Locale('nl'),
  170. Locale('en'),
  171. ],
  172. theme: ThemeData(
  173. brightness: Brightness.light,
  174. ),
  175. themeMode: _themeMode,
  176. routerConfig: _router,
  177. );
  178. }
  179. }