Skip to content

Commit

Permalink
chore(firebase_crashlytics): fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Jan 18, 2021
1 parent 40c5b90 commit 626c579
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 195 deletions.
192 changes: 103 additions & 89 deletions packages/firebase_crashlytics/firebase_crashlytics/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ import 'package:flutter/material.dart';

// Toggle this to cause an async error to be thrown during initialization
// and to test that runZonedGuarded() catches the error
final _kShouldTestAsyncErrorOnInit = false;
const _kShouldTestAsyncErrorOnInit = false;

// Toggle this for testing Crashlytics in your app locally.
final _kTestingCrashlytics = true;
const _kTestingCrashlytics = true;

main() {
void main() {
WidgetsFlutterBinding.ensureInitialized();
runZonedGuarded(() {
runApp(MyApp());
}, (error, stackTrace) {
print('runZonedGuarded: Caught error in my root zone.');
FirebaseCrashlytics.instance.recordError(error, stackTrace);
});
}

// ignore: public_member_api_docs
class MyApp extends StatefulWidget {
// ignore: public_member_api_docs
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
Expand All @@ -41,6 +42,7 @@ class _MyAppState extends State<MyApp> {
Future<void> _testAsyncErrorOnInit() async {
Future<void>.delayed(const Duration(seconds: 2), () {
final List<int> list = <int>[];
// ignore: avoid_print
print(list[100]);
});
}
Expand Down Expand Up @@ -100,103 +102,115 @@ class _MyAppState extends State<MyApp> {
child: Column(
children: <Widget>[
RaisedButton(
child: const Text('Key'),
onPressed: () {
FirebaseCrashlytics.instance
.setCustomKey('example', 'flutterfire');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Custom Key "example: flutterfire" has been set \n'
'Key will appear in Firebase Console once app has crashed and reopened'),
duration: Duration(seconds: 5),
));
}),
onPressed: () {
FirebaseCrashlytics.instance
.setCustomKey('example', 'flutterfire');
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text(
'Custom Key "example: flutterfire" has been set \n'
'Key will appear in Firebase Console once app has crashed and reopened'),
duration: Duration(seconds: 5),
));
},
child: const Text('Key'),
),
RaisedButton(
child: const Text('Log'),
onPressed: () {
FirebaseCrashlytics.instance
.log('This is a log example');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'The message "This is a log example" has been logged \n'
'Message will appear in Firebase Console once app has crashed and reopened'),
duration: Duration(seconds: 5),
));
}),
onPressed: () {
FirebaseCrashlytics.instance
.log('This is a log example');
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text(
'The message "This is a log example" has been logged \n'
'Message will appear in Firebase Console once app has crashed and reopened'),
duration: Duration(seconds: 5),
));
},
child: const Text('Log'),
),
RaisedButton(
child: const Text('Crash'),
onPressed: () async {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('App will crash is 5 seconds \n'
'Please reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));

// Delay crash for 5 seconds
sleep(const Duration(seconds: 5));

// Use FirebaseCrashlytics to throw an error. Use this for
// confirmation that errors are being correctly reported.
FirebaseCrashlytics.instance.crash();
}),
onPressed: () async {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text('App will crash is 5 seconds \n'
'Please reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));

// Delay crash for 5 seconds
sleep(const Duration(seconds: 5));

// Use FirebaseCrashlytics to throw an error. Use this for
// confirmation that errors are being correctly reported.
FirebaseCrashlytics.instance.crash();
},
child: const Text('Crash'),
),
RaisedButton(
child: const Text('Throw Error'),
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Thrown error has been caught \n'
'Please crash and reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));

// Example of thrown error, it will be caught and sent to
// Crashlytics.
throw StateError('Uncaught error thrown by app');
}),
onPressed: () {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text('Thrown error has been caught \n'
'Please crash and reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));

// Example of thrown error, it will be caught and sent to
// Crashlytics.
throw StateError('Uncaught error thrown by app');
},
child: const Text('Throw Error'),
),
RaisedButton(
child: const Text('Async out of bounds'),
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Uncaught Exception that is handled by second parameter of runZonedGuarded \n'
onPressed: () {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text(
'Uncaught Exception that is handled by second parameter of runZonedGuarded \n'
'Please crash and reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));

// Example of an exception that does not get caught
// by `FlutterError.onError` but is caught by
// `runZonedGuarded`.
runZonedGuarded(() {
Future<void>.delayed(const Duration(seconds: 2),
() {
final List<int> list = <int>[];
// ignore: avoid_print
print(list[100]);
});
}, FirebaseCrashlytics.instance.recordError);
},
child: const Text('Async out of bounds'),
),
RaisedButton(
onPressed: () async {
try {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text('Recorded Error \n'
'Please crash and reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));

// Example of an exception that does not get caught
// by `FlutterError.onError` but is caught by
// `runZonedGuarded`.
runZonedGuarded(() {
Future<void>.delayed(const Duration(seconds: 2),
() {
final List<int> list = <int>[];
print(list[100]);
});
}, FirebaseCrashlytics.instance.recordError);
}),
RaisedButton(
child: const Text('Record Error'),
onPressed: () async {
try {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(
content: Text('Recorded Error \n'
'Please crash and reopen to send data to Crashlytics'),
duration: Duration(seconds: 5),
));
throw 'error_example';
} catch (e, s) {
// "reason" will append the word "thrown" in the
// Crashlytics console.
await FirebaseCrashlytics.instance
.recordError(e, s, reason: 'as an example');
}
}),
throw Error();
} catch (e, s) {
// "reason" will append the word "thrown" in the
// Crashlytics console.
await FirebaseCrashlytics.instance
.recordError(e, s, reason: 'as an example');
}
},
child: const Text('Record Error'),
),
],
),
);
break;
default:
return Center(child: Text('Loading'));
return const Center(child: Text('Loading'));
}
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ environment:
flutter: ">=1.12.13+hotfix.5 <2.0.0"

dependencies:
flutter:
sdk: flutter
firebase_crashlytics:
path: ../
firebase_core:
path: ../../../firebase_core/firebase_core

dependency_overrides:
firebase_crashlytics_platform_interface:
path: ../../firebase_crashlytics_platform_interface
firebase_crashlytics:
path: ../
flutter:
sdk: flutter

dependency_overrides:
firebase_core:
path: ../../../firebase_core/firebase_core
firebase_core_platform_interface:
path: ../../../firebase_core/firebase_core_platform_interface
firebase_crashlytics:
path: ../
firebase_crashlytics_platform_interface:
path: ../../firebase_crashlytics_platform_interface
quiver: ">=3.0.0-nullsafety.2 <4.0.0"

dev_dependencies:
pedantic: ^1.8.0
drive: 0.0.1-6.0.pre
e2e: ^0.6.1
flutter_driver:
sdk: flutter
pedantic: ^1.8.0
test: any
e2e: ^0.6.1

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ void testsMain() {
await crashlytics.setCrashlyticsCollectionEnabled(true);
try {
await crashlytics.checkForUnsentReports();
fail("Error did not throw");
fail('Error did not throw');
} catch (e) {
print(e);
// Do nothing. test will fail.
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ part of firebase_crashlytics;
///
/// You can get an instance by calling [FirebaseCrashlytics.instance].
class FirebaseCrashlytics extends FirebasePluginPlatform {
FirebaseCrashlytics._({this.app})
: super(app.name, 'plugins.flutter.io/firebase_crashlytics');

/// Cached instance of [FirebaseCrashlytics];
static /*late*/ FirebaseCrashlytics _instance;

Expand All @@ -26,9 +29,6 @@ class FirebaseCrashlytics extends FirebasePluginPlatform {
/// The [FirebaseApp] for this current [FirebaseCrashlytics] instance.
FirebaseApp app;

FirebaseCrashlytics._({this.app})
: super(app.name, 'plugins.flutter.io/firebase_crashlytics');

/// Returns an instance using the default [FirebaseApp].
static FirebaseCrashlytics get instance {
return _instance ??= FirebaseCrashlytics._(app: Firebase.app());
Expand Down Expand Up @@ -87,33 +87,40 @@ class FirebaseCrashlytics extends FirebasePluginPlatform {
: (StringBuffer()..writeAll(information, '\n')).toString();

if (printDetails) {
// ignore: avoid_print
print('----------------FIREBASE CRASHLYTICS----------------');

// If available, give a reason to the exception.
if (reason != null) {
// ignore: avoid_print
print('The following exception was thrown $reason:');
}

// Need to print the exception to explain why the exception was thrown.
// ignore: avoid_print
print(exception);

// Print information provided by the Flutter framework about the exception.
// ignore: avoid_print
if (_information.isNotEmpty) print('\n$_information');

// Not using Trace.format here to stick to the default stack trace format
// that Flutter developers are used to seeing.
// ignore: avoid_print
if (stack != null) print('\n$stack');
// ignore: avoid_print
print('----------------------------------------------------');
}

// The stack trace can be null. To avoid the following exception:
// Invalid argument(s): Cannot create a Trace from null.
// We can check for null and provide an empty stack trace.
stack ??= StackTrace.current ?? StackTrace.fromString('');
final StackTrace stackTrace =
stack ?? StackTrace.current ?? StackTrace.fromString('');

// Report error.
final List<String> stackTraceLines =
Trace.format(stack).trimRight().split('\n');
Trace.format(stackTrace).trimRight().split('\n');
final List<Map<String, String>> stackTraceElements =
getStackTraceElements(stackTraceLines);

Expand Down
Loading

0 comments on commit 626c579

Please sign in to comment.