How to crash Flutter App for Android and iOS (to test Firebase Crashlytics)? - firebase

I want to test out crash report using Firebase Crashlytics in my Flutter App. I need to Fatal crash my flutter app for both Android and iOS programmatically. Any idea?

If you have the flutter_crashlytics plugin, you can test it using
FirebaseCrashlytics.instance.crash();

You should be able to throw a Dart exception by doing this anywhere in your Flutter app:
throw Exception("This is a crash!");
Or by using an arbitrary object:
throw "This is a crash!";
You can find more info about dart exceptions in the language tour and if you want, you can create your own custom Exception type as explained in this SO answer

integrate firebase_crashlytics
Crashlytics.instance.crash();

using firebase_crashlytics plugin
FirebaseCrashlytics.instance.crash();
You should enable crashlytics
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
if (kDebugMode) {
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);//disable false
}else{
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
}

You can crash you Flutter app by many ways:
throw(Exception('Hello Crashlytics')); like in #Xavi Rigau's answer
List list;
list.add("add");
Like #Paresh Mangukiya does.
3.
final a = [12];
print(a[1]);
But there is 2 IMPORTANT thing you should consider:
You should run your app in release mode, for example with the command flutter run --release
In Crashlytycs console, by default shown only fatal crashes. The crashes above are non-fatal crashes. So, you should clear the filter to see all crash types. (Filter can be filtered in crashlytics dashboard by clicking (x) button near filter). More on this you can read from this SO answer

Check this out . This is from the official documentation. https://firebase.flutter.dev/docs/crashlytics/usage
Once your configuration is proper according to documentation for crashlytics, FirebaseCrashlytics.instance.crash(); will crash the app.

Related

How to test a Flutter application using Firebase?

I am using Flutter and Firebase as my database. I want to do some unit test of my application, but when I start this test :
testWidgets('MyWidget has a title and message', (WidgetTester tester) async {
// Create the widget by telling the tester to build it.
await tester.pumpWidget(LoginPage());
expect(true, true);
});
I have the Exception :
No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
But Firebase.initializeApp() is called in my main.dart here :
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
And I don't know how to initialize the Firebase app in my test.
Posting this as a community wiki as it's based on #GuilhermeGabanelli's comment.
If you check this flutterfire documentation on how to perform unit tests with Firebase Services you will see that:
The Firebase libraries need to run on an actual device or emulator. So if you want to run unit tests, you'll have to use Fakes instead. A Fake is a library that implements the API of a given Firebase library and simulates its behavior.
...
When initializing your app, instead of passing the actual instance of a Firebase library (e.g. FirebaseFirestore.instance if using Firestore), you pass an instance of a fake (e.g. FakeFirebaseFirestore()). Then the rest of your application will run as if it were talking to Firebase.
Followed by an example with sample code. I believe this is the cause of the issue you are facing and if you change your code to use Fakes this should be fixed.

Flutter Crashlytics - No Crash log for non-fatal flutter error

We are using firebase_crashlytics: ^0.4.0+1 in a flutter app. Within the runApp() we are starting to listen to errors and crashes just like mentioned in the documentation.
runZonedGuarded(() {
runApp(
..
}, (error, stackTrace) {
FirebaseCrashlytics.instance.recordError(error, stackTrace);
});
In general crashes are reported, but not for these type of crashes EXCEPTION CAUGHT BY WIDGETS LIBRARY.
In some circumstances objects might be null, but instead of getting a crash reported to the firebase crashlytics dashboard, nothing happens. User only receive a grey screen and that is it.
Therefore we only are notified if a user reports these errors, but we receive no logs.
Is this a non-fatal error and we should be using recordFlutterError method?
BTW, this is being called when the error is thrown:
Function originalOnError = FlutterError.onError;
FlutterError.onError = (FlutterErrorDetails errorDetails) async {
await FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
// Forward to original handler.
originalOnError(errorDetails);
Even the method originalOnError(errorDetails) is called the error is not recorded in the firebase crashlytics dashboard.
How can we make these non-fatal errors also be reported?
The logging has been done by firebase crashlytics. The default filter in the firebase console dashboard was set to crashes. Removing the filter and we can see all reported errors, crashes, non-fatal errors.
Thanks to this stackoverflow post

Flutter Firebase Crashlytics reports stack trace as Java class files and not as Dart files

I have integrated Flutter Crashlytics in our app, but it is reporting crashes stack trace in Java class files and not in Dart files. So it is difficult to infer what is the file and line number where this issue occurred in the flutter codebase.
Here is an example of one such crash report from Firebase Crashltytics of an Uncaught exception:
Non-fatal Exception: java.lang.Exception: NoSuchMethodError: The method 'markNeedsBuild' was called on null.
Receiver: null
Tried calling: markNeedsBuild()
at State.setState(State.java:1168)
at _ArgonButtonState.initState.<fn>(initState.java:107)
at AnimationLocalStatusListenersMixin.notifyStatusListeners(AnimationLocalStatusListenersMixin.java:193)
at AnimationController._checkStatusChanged(AnimationController.java:773)
at AnimationController._tick(AnimationController.java:789)
at Ticker._tick(Ticker.java:237)
at SchedulerBinding._invokeFrameCallback(SchedulerBinding.java:1102)
at SchedulerBinding.handleBeginFrame.<fn>(handleBeginFrame.java:1017)
at SchedulerBinding.handleBeginFrame(SchedulerBinding.java:1015)
at SchedulerBinding._handleBeginFrame(SchedulerBinding.java:949)
Here is how Crashlytics is initialized:
// Pass all uncaught errors from the framework to Crashlytics.
FlutterError.onError = Crashlytics.instance.recordFlutterError;
runZoned(() {
runApp(AppMain(homeWidget));
}, onError: Crashlytics.instance.recordError);
I am reporting caught exceptions as such:
Crashlytics.instance.recordError(error, stack);
How can I configure Crashlytics to report stack trace in dart? Does Crashlytics show erroneous file names and line numbers for Dart source code?
Todd from Crashlytics. This is a known behavior and we are looking into possible options moving forward. Kotlin users will see a similar behavior.
I would suggest to move on to Sentry.
I am doing it for my app now. And while just testing it, I can say that sentry is much better than Crashlytics!

Error : FacebookSDK has not been initialized yet using react native

So I am trying to add a facebook sign up connect to firebase to my react-native application and expo keep telling me this error . I dont really know what to do because I thought that Facebook SDK was auto initialized on Application start.
This is my Sign In function.
According to the documentation: https://docs.expo.io/versions/latest/sdk/facebook/, you need to initialize the SDK with this function:
Facebook.initializeAsync(appId: string | undefined, appName: string | undefined): Promise<void>
Calling this method ensures that the SDK is initialized. You have to call this method before calling logInWithReadPermissionsAsync to ensure that Facebook support is initialized properly.
You should call this method in your App.js or any top level component.
Came here from Github, after upgrading from Expo SDK 35 => 36 ran into this issue.
Adding this line to app.json fixed it:
"facebookAutoInitEnabled": true
From docs: https://docs.expo.io/versions/latest/sdk/facebook/
The above answer may work as well, but this fixed it in my case

FieldValue.increment for Cloud Firestore in Flutter

I'm creating a android app using flutter and Firebase Firestore.
I would like to add that new FieldValue.increment functionality which is apperently available in Firebase since this april but i've got an error:
The method 'increment' isn't defined for the class 'FieldValue'.
Here is my code that i've tried:
onTap: (){
Firestore.instance.document('docRef').updateData({"numberOfDocs": FieldValue.increment(1)});
}
I'm just a beginner programmer but when i checked that field_value.dart file it's missing "increment" implementation.
So, is it right answer to say that flutter team didn't yet implemented that functionality?
I've seen a tutorial where increment is already used in some .js code but for me it's not working.
https://www.youtube.com/watch?v=8Ejn1FLRRaw
This has been added to the cloud_firestore Flutter plugin as of version 0.10.0.
You can use FieldValue.increment with a double (e.g. FieldValue.increment(1.0)) or an int (e.g. FieldValue.increment(2)).
Here is the relevant pull request.
If anyone reads this before the version is pushed to Pub: The link to the version will not yet work, but in the meantime you can use this Git commit, which contains a fix I did.
User that Import:
import 'package:cloud_firestore/cloud_firestore.dart';
For Increment:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(1)});
For Decrement:
await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(-1)});
As far as I can see in the change log for FlutterFire, the increment operator has not been added yet.
I added a feature request to the repo.

Resources