Flutterfire: [cloud_firestore 0.14.0+2] Null pointer exception on ListenerRegistration.remove()

Created on 26 Aug 2020  Â·  15Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
So I just got this error while doing a hot restart (not reload):

Caught error: PlatformException(firebase_core, java.util.concurrent.ExecutionException: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.firebase.firestore.ListenerRegistration.remove()' on a null object reference, null)
I/flutter (25308): #0      StandardMethodCodec.decodeEnvelope 
package:flutter/…/services/message_codecs.dart:572
I/flutter (25308): #1      MethodChannel._invokeMethod 
package:flutter/…/services/platform_channel.dart:161
I/flutter (25308): <asynchronous suspension>
I/flutter (25308): #2      MethodChannel.invokeMethod 
package:flutter/…/services/platform_channel.dart:334
I/flutter (25308): #3      MethodChannel.invokeListMethod 
package:flutter/…/services/platform_channel.dart:347
I/flutter (25308): #4      MethodChannelFirebase._initializeCore 
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:30
I/flutter (25308): #5      MethodChannelFirebase.initializeApp 
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:75
I/flutter (25308): #6      Firebase.initializeApp 
package:firebase_core/src/firebase.dart:43
I/flutter (25308): #7      main.<anonymous closure> 
package:tillty/main.dart:48
I/flutter (25308): #8      main.<anonymous closure> 
package:tillty/main.dart:46
I/flutter (25308): #9      _rootRun  (dart:async/zone.dart:1190:13)
I/flutter (25308): #10     _CustomZone.run  (dart:async/zone.dart:1093:19)
I/flutter (25308): #11     _

After it happened, I couldn't reload the app in any way, as the error persisted, and had to shut it down and restart it.

To Reproduce
Not sure how to reproduce it, as I've hot restarted before without issue.

Expected behavior
That it doesn't do that.

Additional context
I ran into a UI crash (the red screen), but it was unrelated to the data layer.
To get out of it I hot restarted.

I'm mentioning this even though I think it's entirely unrelated.

Flutter doctor

[✓] Flutter (Channel stable, 1.20.2, on Mac OS X 10.13.6 17G14019, locale en-DK)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.48.2)
[✓] Connected device (1 available)
cloud_firestore bug

All 15 comments

Hi @larssn,
Can you provide pubspec.yaml and a minimal reproducible code sample that could possibly throw this exception ?
Thanks.

I can only supply the pubspec for now, as I dont know how to reproduce it:

environment:
  sdk: ">=2.9.1 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  flutter_cupertino_localizations: ^1.0.1

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3

  animated_text_kit: ^2.2.0
  audioplayers: ^0.15.1
  bloc: ^6.0.0
  built_collection: ^4.3.2
  built_value: ^7.1.0
  cached_network_image: ^2.2.0+1
  charts_flutter: ^0.9.0
  cloud_firestore: ^0.14.0
  country_code_picker: ^1.3.15
  country_pickers: ^1.3.0
  crypto: ^2.1.5
  data_connection_checker: ^0.3.4
  device_info: ^0.4.2+4
  firebase_auth: ^0.18.0
  firebase_core: "^0.5.0"
  firebase_crashlytics: ^0.1.4+1
  firebase_dynamic_links: ^0.6.0
  firebase_storage: ^4.0.0
  flutter_bloc: ^6.0.0
  flutter_dotenv: ^2.1.0
  flutter_material_color_picker: ^1.0.5
  flutter_nfc_acs:
    git:
      url: https://github.com/nuvoPoint/flutter_nfc_acs
  flutter_slidable: ^0.5.5
  flutter_vibrate: ^1.0.0
  http: ^0.12.2
  image_picker: ^0.6.7+1
  image: ^2.1.13
  intl: ^0.16.1
  nfc_in_flutter: ^2.0.4
  package_info: ^0.4.1
  phone_number: ^0.6.2+4
  positioned_tap_detector: ^1.0.3
  provider: ^4.1.3
  random_string: ^2.0.1
  reorderables: ^0.3.2
  rxdart: ^0.24.1
  sentry: ^3.0.1
  shared_preferences: ^0.5.7+3
  sprintf: ^4.0.2
  strings: ^0.1.2
  time: ^1.3.0
  url_launcher: ^5.4.10
  uuid: ^2.0.4
  validators: ^2.0.0+1

dev_dependencies:
  # flutter_test:
  #   sdk: flutter

  build_runner: ^1.10.0
  built_value_generator: ^7.1.0
  intl_translation: ^0.17.10

Try upgrading to cloud_firestore 0.14.0+2 - I think this has been fixed already.

As I wrote in the title, I'm already using that version. I can give you the pubspec.lock file, if needed.

Well I seem to, unless the pubspec.lock file is incorrect.

But the line crashing was ListenerRegistration.remove() which is even weirder. Maybe it's just the way hot restarting works, that can cause issues like this.

@larssn
Can you try modifying this

private void removeEventListeners() {
    for (int i = 0; i < listenerRegistrations.size(); i++) {
      int key = listenerRegistrations.keyAt(i);
      listenerRegistrations.get(key).remove();
    }
    listenerRegistrations.clear();
  }

To something like this

private void removeEventListeners() {
    for (int i = 0; i < listenerRegistrations.size(); i++) {
      int key = listenerRegistrations.keyAt(i);
      if(listenerRegistrations.get(key) !=null){
          listenerRegistrations.get(key).remove();
       }

    }
    listenerRegistrations.clear();
  }

I am having similar issue. Am completely clueless of what to do

Fix this ASAP please running into trouble in production

@itssidhere Did that code you put above work?

@Ehesp I cannot reproduce but it's happening in production as you can see from the attachment.

I solve the problem by editing the removeEventListeners method of cloud_firestore.
thanks to @itssidhere

private void removeEventListeners() {
    try{
        for (int i = 0; i < listenerRegistrations.size(); i++) {
            int key = listenerRegistrations.keyAt(i);
            if(listenerRegistrations.get(key) != null)
                listenerRegistrations.get(key).remove();
        }
        listenerRegistrations.clear();
    }
    catch(Exception e){}
  }

@Ehesp my solution worked for him.

Ah, I see what's going on. It's been implemented as a SparseArray - will sort tomorrow.

Was this page helpful?
0 / 5 - 0 ratings