From c54d1b88a9a30ee8f8399325c83c1e23d02a8645 Mon Sep 17 00:00:00 2001 From: mehul425 Date: Thu, 20 Jul 2023 10:57:08 +0530 Subject: [PATCH] Disable crashlytics and analytics by default --- README.md | 34 ++++++++++++++++++++++++++++++++++ lib/main.dart | 41 ++++++++++++++++++++++++++++------------- lib/src/ui/app/app.dart | 7 ++++--- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 016b815..43c24ff 100755 --- a/README.md +++ b/README.md @@ -50,6 +50,40 @@ Overall all this puzzle tries to engage you in a different way to improve your m **2) Picture Puzzle** : Each shape represents a number. You need find number related to each shape and solve the last equation.
**3) Number Pyramid** : In a number pyramid, the numbers on the lower layers determine the numbers above them. Sum of two consecutive cell would be placed on top cell.
+## Crashlytics and Analytics +To enable Firebase Crashlytics and Analytics, do the following: + +1. Create a new project in + [console.firebase.google.com](https://console.firebase.google.com/). + Call the Firebase project whatever you like; just **remember the name**. + You don't need to enable Analytics in the project if you don't want to. +2. [Install `firebase-tools`](https://firebase.google.com/docs/cli?authuser=0#setup_update_cli) + on your machine. +3. [Install `flutterfire` CLI](https://firebase.flutter.dev/docs/cli#installation) + on your machine. +4. In the root of this project (the directory containing `pubspec.yaml`), + run the following: + - `flutterfire configure` + - This command asks you for the name of the Firebase project + that you created earlier, and the list of target platforms you support. + As of April 2022, only `android` and `ios` are fully + supported by Crashlytics. + - The command rewrites `lib/firebase_options.dart` with + the correct code. +5. Go to `lib/main.dart` and uncomment the lines that relate to Crashlytics and Analytics. + +You should now be able to see crashes, errors, and +severe log messages in +[console.firebase.google.com](https://console.firebase.google.com/). +To test, add a button to your project, and throw whatever +exception you like when the player presses it. + +```dart +TextButton( + onPressed: () => throw StateError('whoa!'), + child: Text('Test Crashlytics'), +) +``` ## Built With This application built [Flutter](https://flutter.dev/). Flutter is cross-platform open source mobile framework built by Google. Flutter use Dart as a primary language which is highly scalable and easy codebase. diff --git a/lib/main.dart b/lib/main.dart index 06d1bf0..33a02ad 100755 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,17 +1,19 @@ import 'package:firebase_analytics/firebase_analytics.dart'; -import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_crashlytics/firebase_crashlytics.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_animate/flutter_animate.dart'; import 'package:get_it/get_it.dart'; -import 'package:mathgame/firebase_options.dart'; import 'package:mathgame/src/ui/app/app.dart'; import 'package:mathgame/src/ui/app/theme_provider.dart'; import 'package:mathgame/src/ui/dashboard/dashboard_provider.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; +// Uncomment the following lines when enabling Firebase Crashlytics +// import 'package:firebase_core/firebase_core.dart'; +// import 'package:mathgame/firebase_options.dart'; + Future main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -19,23 +21,36 @@ Future main() async { Animate.restartOnHotReload = true; } - await Firebase.initializeApp( - options: DefaultFirebaseOptions.currentPlatform, - ); + FirebaseAnalytics? firebaseAnalytics; + FirebaseCrashlytics? crashlytics; - FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.instance; + // To enable Firebase Crashlytics and Analytics, uncomment the following lines and + // the import statements at the top of this file. + // See the 'Crashlytics and Analytics' section of the main README.md file for details. + + // try { + // await Firebase.initializeApp( + // options: DefaultFirebaseOptions.currentPlatform, + // ); + // firebaseAnalytics = FirebaseAnalytics.instance; + // crashlytics = FirebaseCrashlytics.instance; + // } catch (e) { + // debugPrint("Firebase couldn't be initialized: $e"); + // } if (kDebugMode) { - await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false); - await firebaseAnalytics.setAnalyticsCollectionEnabled(false); + await crashlytics?.setCrashlyticsCollectionEnabled(false); + await firebaseAnalytics?.setAnalyticsCollectionEnabled(false); } - FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError; + if (crashlytics != null) { + FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError; - PlatformDispatcher.instance.onError = (error, stack) { - FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); - return true; - }; + PlatformDispatcher.instance.onError = (error, stack) { + FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); + return true; + }; + } final sharedPreferences = await SharedPreferences.getInstance(); diff --git a/lib/src/ui/app/app.dart b/lib/src/ui/app/app.dart index b4655d3..b6f5b89 100644 --- a/lib/src/ui/app/app.dart +++ b/lib/src/ui/app/app.dart @@ -2,14 +2,14 @@ import 'package:firebase_analytics/firebase_analytics.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:mathgame/src/core/app_constant.dart'; -import 'package:mathgame/src/core/app_theme.dart'; import 'package:mathgame/src/core/app_routes.dart'; +import 'package:mathgame/src/core/app_theme.dart'; import 'package:mathgame/src/ui/app/theme_provider.dart'; import 'package:provider/provider.dart'; class MyApp extends StatelessWidget { final String fontFamily = "Montserrat"; - final FirebaseAnalytics firebaseAnalytics; + final FirebaseAnalytics? firebaseAnalytics; const MyApp({ required this.firebaseAnalytics, @@ -35,7 +35,8 @@ class MyApp extends StatelessWidget { routes: appRoutes, // home: DashboardView(), navigatorObservers: [ - FirebaseAnalyticsObserver(analytics: firebaseAnalytics) + if (firebaseAnalytics != null) + FirebaseAnalyticsObserver(analytics: firebaseAnalytics!) ], ); });