firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.62k stars 3.95k forks source link

πŸ› [cloud_firestore] Attempt to invoke virtual method on a null object reference #4361

Closed DeanPack closed 3 years ago

DeanPack commented 3 years ago

Bug report

Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.invokeMethod(java.lang.String, java.lang.Object)' on a null object reference io.flutter.plugins.firebase.firestore.FlutterFirebaseFirestorePlugin.a I get this bug from crashlytics. I cannot replicate it on any of my devices, but in reviewing your code I can tell you where it is happening at least. in FlutterFirebaseFirestorePlugin.java there are several calls to channel.invokeMethod(), it is crashing here because for some reason channel is null. I also noticed that the channel is set to null in the onDetachedFromEngine function.

Steps to reproduce

I haven't reproduced this error but I know it has something to do with these lines of code: @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { removeEventListeners(); channel.setMethodCallHandler(null); channel = null; } channel.invokeMethod("QuerySnapshot#error", querySnapshotMap); the channel is reset when onAttachedToEngine is called, so there must be something invoking a method on the channel when it is in background mode or before onAttachedToEngine is called.


Flutter doctor

Run flutter doctor and paste the output below:

[βœ“] Flutter (Channel stable, 1.22.4, on Mac OS X 10.15.6 19G2021 darwin-x64, locale en-US)

[βœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.2) [βœ“] Xcode - develop for iOS and macOS (Xcode 12.2) [βœ“] Android Studio (version 3.4) [βœ“] VS Code (version 1.51.1) [βœ“] Connected device (1 available)

β€’ No issues found!

Flutter dependencies

Dart SDK 2.10.4 Flutter SDK 1.22.4 hallo 0.0.0

dependencies:

dev dependencies:

transitive dependencies:

darshankawar commented 3 years ago

@DeanPack This sounds very similar to https://github.com/FirebaseExtended/flutterfire/issues/2517 and https://github.com/FirebaseExtended/flutterfire/issues/2390. These issues were fixed by this PR, but will need to confirm if this has regressed.

If possible, can you try to check if you are able to replicate the crash with any of the scenarios mentioned in below comments:

https://github.com/FirebaseExtended/flutterfire/issues/2517#issuecomment-626857499

https://github.com/FirebaseExtended/flutterfire/issues/2517#issuecomment-626869363

https://github.com/FirebaseExtended/flutterfire/issues/2517#issuecomment-629701893

If so or in any case, it would be good to have a reproducible code / usage that'll help to analyze the issue better.

DeanPack commented 3 years ago

Thank you for your quick response. I've tried replicating the crash with the steps from these comments, but was unable to. Here is what I tried

`    switch (lifeState) {
      case AppLifecycleState.inactive:
        print("PHONE INACTIVE");
        break;
      case AppLifecycleState.paused:
        print("PHONE PAUSED");
        widget.model.leavingApp();
        break;
      case AppLifecycleState.detached:
        print("PHONE DETACHED");
        break;
      case AppLifecycleState.resumed:
        print("PHONE RESUMED");
        await FirebaseFirestore.instance
        .collection('users')
        .doc(UserModel.of(context).currentUser.id)
        .set({'test': 'foo'},SetOptions(merge: true));
        widget.model.enteringApp();
        checkVersionForForceUpdate(context, showingForceUpdateDialog)
            .then((dialogShowing) => showingForceUpdateDialog = dialogShowing);
        break;
    }`

I have a stream for my users document, and temporarily added this line that does a write when the app is coming from the background. I didn't get any crashes, but the write also didn't work, and I didn't get any error messages.

darshankawar commented 3 years ago

I am unable to replicate this issue after running the official cloud_firestore plugin example on latest stable. Keeping this issue open for further analysis to confirm if this is a regression or not.

Cc @Ehesp

Ehesp commented 3 years ago

If I were to take a guess, this error is probably tricky to replicate because it'll happen after some native event (e.g. closing and reopening the app, minimising and opening, phone removing the app due to memory etc).

Puspharaj commented 3 years ago

Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'void c.a.c.a.j.a(java.lang.String, java.lang.Object, c.a.c.a.j$d)' on a null object reference io.flutter.plugins.firebase.auth.FlutterFirebaseAuthPlugin$3.a

Same error here! I'm using phone authentication here. When a user enters phone no and then this issue pops up silently and caught by firebase. Test users with phone only works here.

One major observation is it's not happening in development mode, only occurs in deployment(Google play store download app).

google_sign_in: ^4.5.4 firebase_auth: ^0.18.4+1 firebase_core: "^0.5.3" firebase_crashlytics: "^0.2.4" cloud_firestore: "^0.14.4"

Android 10.

Please tell us a workaround.

Thanks

DeanPack commented 3 years ago

no response in over 3 weeks. This bug alone took our crash free users from 99.6% to 95%. This should be a top priority for you

Ehesp commented 3 years ago

@DeanPack work is being done on https://github.com/FirebaseExtended/flutterfire/pull/4209 which looks like the same issue.

DeanPack commented 3 years ago

That looks promising! Thank you for the update

russellwheatley commented 3 years ago

4209 was released a few months ago. I'm going to assume that this has improved performance. Will reopen if this is not the case.