Skip to content

Fix crashlytics in game_template #1998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions game_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ lib
│   ├── ads
│   ├── app_lifecycle
│   ├── audio
│   ├── crashlytics
│   ├── game_internals
│   ├── games_services
│   ├── in_app_purchase
Expand Down Expand Up @@ -240,12 +239,9 @@ with their mouth.

## Crashlytics

Crashlytics integration is disabled by default. But even if you don't
enable it, you might find code in `lib/src/crashlytics` helpful.
It gathers all log messages and errors, so that you can, at the very least,
print them to the console.
Crashlytics integration is disabled by default.

When enabled, this integration is a lot more powerful:
When enabled, this integration is quite powerful:

- Any crashes of your app are sent to the Firebase Crashlytics console.
- Any uncaught exception thrown anywhere in your code is captured
Expand All @@ -256,14 +252,6 @@ When enabled, this integration is a lot more powerful:
- Device model, orientation, RAM free, disk free
- Operating system version
- App version
- In addition, log messages generated anywhere in your app
(and from packages you use) are recorded in memory,
and are sent alongside the reports. This means that you can
learn what happened before the crash or exception
occurred.
- Also, any generated log message with `Level.severe` or above
is also sent to Crashlytics.
- You can customize these behaviors in `lib/src/crashlytics`.

To enable Firebase Crashlytics, do the following:

Expand All @@ -273,7 +261,7 @@ To enable Firebase Crashlytics, do the following:
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)
3. [Install `flutterfire` CLI](https://firebase.google.com/docs/flutter/setup)
on your machine.
4. In the root of this project (the directory containing `pubspec.yaml`),
run the following:
Expand All @@ -286,8 +274,7 @@ To enable Firebase Crashlytics, do the following:
the correct code.
5. Go to `lib/main.dart` and uncomment the lines that relate to Crashlytics.

You should now be able to see crashes, errors, and
severe log messages in
You should now be able to see crashes and errors 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.
Expand Down
48 changes: 31 additions & 17 deletions game_template/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
// Uncomment the following lines when enabling Firebase Crashlytics
// import 'dart:io';
// import 'package:firebase_core/firebase_core.dart';
// import 'package:firebase_crashlytics/firebase_crashlytics.dart';
// import 'package:flutter/foundation.dart';
// import 'firebase_options.dart';

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'dart:developer' as dev;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
Expand All @@ -17,7 +20,6 @@ import 'package:provider/provider.dart';
import 'src/ads/ads_controller.dart';
import 'src/app_lifecycle/app_lifecycle.dart';
import 'src/audio/audio_controller.dart';
import 'src/crashlytics/crashlytics.dart';
import 'src/games_services/games_services.dart';
import 'src/games_services/score.dart';
import 'src/in_app_purchase/in_app_purchase.dart';
Expand All @@ -38,33 +40,45 @@ import 'src/style/snack_bar.dart';
import 'src/win_game/win_game_screen.dart';

Future<void> main() async {
// To enable Firebase Crashlytics, uncomment the following lines and
// the import statements at the top of this file.
// Subscribe to log messages.
Logger.root.onRecord.listen((record) {
dev.log(
record.message,
time: record.time,
level: record.level.value,
name: record.loggerName,
zone: record.zone,
error: record.error,
stackTrace: record.stackTrace,
);
});

WidgetsFlutterBinding.ensureInitialized();

// TODO: To enable Firebase Crashlytics, uncomment the following line.
// See the 'Crashlytics' section of the main README.md file for details.

FirebaseCrashlytics? crashlytics;
// if (!kIsWeb && (Platform.isIOS || Platform.isAndroid)) {
// try {
// WidgetsFlutterBinding.ensureInitialized();
// await Firebase.initializeApp(
// options: DefaultFirebaseOptions.currentPlatform,
// );
// crashlytics = FirebaseCrashlytics.instance;
//
// FlutterError.onError = (errorDetails) {
// FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
// };
//
// // Pass all uncaught asynchronous errors
// // that aren't handled by the Flutter framework to Crashlytics.
// PlatformDispatcher.instance.onError = (error, stack) {
// FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
// return true;
// };
// } catch (e) {
// debugPrint("Firebase couldn't be initialized: $e");
// }
// }

await guardWithCrashlytics(
guardedMain,
crashlytics: crashlytics,
);
}

/// Without logging and crash reporting, this would be `void main()`.
void guardedMain() {
WidgetsFlutterBinding.ensureInitialized();

_log.info('Going full screen');
SystemChrome.setEnabledSystemUIMode(
SystemUiMode.edgeToEdge,
Expand Down
103 changes: 0 additions & 103 deletions game_template/lib/src/crashlytics/crashlytics.dart

This file was deleted.

6 changes: 3 additions & 3 deletions game_template/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ dependencies:
flutter:
sdk: flutter

audioplayers: ^5.0.0
audioplayers: ^5.1.0
cupertino_icons: ^1.0.2
go_router: ^10.0.0
go_router: ^10.1.2
logging: ^1.1.0
provider: ^6.0.2
shared_preferences: ^2.0.13
Expand All @@ -24,7 +24,7 @@ dependencies:
# delete the relevant line below, and get rid of any Dart code
# that references the dependency.
firebase_core: ^2.1.1 # Needed for Crashlytics below
firebase_crashlytics: ^3.0.3 # Error reporting
firebase_crashlytics: ^3.3.5 # Error reporting
games_services: ^3.0.0 # Achievements and leaderboards
google_mobile_ads: ^3.0.0 # Ads
in_app_purchase: ^3.0.1 # In-app purchases
Expand Down
39 changes: 0 additions & 39 deletions game_template/test/crashlytics_test.dart

This file was deleted.