Skip to content
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

[Bug] NullPointerException when accessing contacts via a Dart Isolate (Background | Android) #232

Closed
zlshames opened this issue Jul 2, 2021 · 8 comments

Comments

@zlshames
Copy link

zlshames commented Jul 2, 2021

Hey all, I'm in the process of migration from Flutter v1 to v2, and one of the things that seems like it broke is my Dart Background Isolate that handles Google FCM notifications. When a notification is received, the isolate gets spun up and the notification is handled, i.e., contacts get fetched, avatars get loaded, etc.

The issue I'm running into is when I fetch contacts from the isolate (even if permissions are granted), I get this error. This error does not occur when the app is open, only when the app is closed and the contacts service is used within the isolate.

There is a similar ticket to this, but it was for iOS. My issue is with Android: #200

Here is the error

2021-07-02 10:05:41.473 14769-14814/com.bluebubbles.messaging E/AndroidRuntime: FATAL EXCEPTION: pool-8-thread-1
    Process: com.bluebubbles.messaging, PID: 14769
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$4.done(AsyncTask.java:415)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.content.res.Resources.getText(int)' on a null object reference
        at android.provider.ContactsContract$CommonDataKinds$Phone.getTypeLabel(ContactsContract.java:6288)
        at flutter.plugins.contactsservice.contactsservice.Item.getPhoneLabel(Item.java:42)
        at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.getContactsFrom(ContactsServicePlugin.java:579)
        at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin.access$300(ContactsServicePlugin.java:55)
        at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask.doInBackground(ContactsServicePlugin.java:444)
        at flutter.plugins.contactsservice.contactsservice.ContactsServicePlugin$GetContactsTask.doInBackground(ContactsServicePlugin.java:420)
        at android.os.AsyncTask$3.call(AsyncTask.java:394)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:923) 

Here is a log showing some prints for when we fetch the contacts from the isolate, and showing that permissions are granted, the the error:

I/flutter (15487): Database Opened
I/flutter (15487): ContactManager -> Checking Permissions...
I/flutter (15487): ContactManager -> We are checking access
I/flutter (15487): ContactManager -> Status: PermissionStatus.granted
I/flutter (15487): ContactManager -> Fetching contacts
E/AndroidRuntime(15487): FATAL EXCEPTION: pool-14-thread-1
E/AndroidRuntime(15487): Process: com.bluebubbles.messaging, PID: 15487

Here is my flutter doctor:

[√] Flutter (Channel stable, 2.2.2, on Microsoft Windows [Version 10.0.19043.1081], locale en-US)
    • Flutter version 2.2.2 at E:\src\flutter
    • Framework revision d79295af24 (3 weeks ago), 2021-06-11 08:56:01 -0700
    • Engine revision 91c9fc8fe0
    • Dart version 2.13.3

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at E:\AppData\Local\Android\Sdk
    • Platform android-30, build-tools 30.0.3
    • ANDROID_SDK_ROOT = E:\AppData\Local\Android\Sdk
    • Java binary at: C:\Program Files\Java\jdk1.8.0_291\bin\java
    • Java version Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[√] VS Code (version 1.57.1)
    • VS Code at C:\Users\zach\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.24.0

[√] Connected device (3 available)
    • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 91.0.4472.124
    • Edge (web)                  • edge          • web-javascript • Microsoft Edge 91.0.864.59

! Doctor found issues in 1 category.

@lukasgit
Copy link
Owner

lukasgit commented Jul 8, 2021

I will look into this issue over the weekend. Thanks!

@zlshames
Copy link
Author

zlshames commented Jul 8, 2021

I will look into this issue over the weekend. Thanks!

Hey, this was solved when we switched from the old FlutterNativeView to using FlutterEngine to start the isolate. I don't believe this is an issue anymore for us. Thanks for replying so I remembered to close it!

@zlshames zlshames closed this as completed Jul 8, 2021
@Fr099y
Copy link

Fr099y commented Aug 16, 2021

Same issue inside Isolate.spawn().
Flutter version: 2.2.0 stable
Dart version: 2.13.0
contacts_service: ^0.6.1

issue :

E/flutter ( 6928): Null check operator used on a null value
E/flutter ( 6928): #0      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
E/flutter ( 6928): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
E/flutter ( 6928): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
E/flutter ( 6928): #3      ContactsService.getContacts (package:contacts_service/contacts_service.dart:24:24)

@jeromeDms
Copy link

I will look into this issue over the weekend. Thanks!

Hey, this was solved when we switched from the old FlutterNativeView to using FlutterEngine to start the isolate. I don't believe this is an issue anymore for us. Thanks for replying so I remembered to close it!

Hi
Could you please describe how did you solve this ?
I'm facing the same issue and I can see multiple crashs on Firebase regarding this issue.
Thanks
Jerome

@zlshames
Copy link
Author

I will look into this issue over the weekend. Thanks!

Hey, this was solved when we switched from the old FlutterNativeView to using FlutterEngine to start the isolate. I don't believe this is an issue anymore for us. Thanks for replying so I remembered to close it!

Hi Could you please describe how did you solve this ? I'm facing the same issue and I can see multiple crashs on Firebase regarding this issue. Thanks Jerome

Hopefully this is helpful, but this is how we use FlutterEngine in our project: https://github.com/BlueBubblesApp/bluebubbles-app/blob/b7ad394233376c29a5081b6e136d90bd7303c17b/android/app/src/main/java/com/bluebubbles/messaging/workers/FCMWorker.java#L108-L109

@jeromeDms
Copy link

jeromeDms commented Apr 13, 2022

I will look into this issue over the weekend. Thanks!

Hey, this was solved when we switched from the old FlutterNativeView to using FlutterEngine to start the isolate. I don't believe this is an issue anymore for us. Thanks for replying so I remembered to close it!

Hi Could you please describe how did you solve this ? I'm facing the same issue and I can see multiple crashs on Firebase regarding this issue. Thanks Jerome

Hopefully this is helpful, but this is how we use FlutterEngine in our project: https://github.com/BlueBubblesApp/bluebubbles-app/blob/b7ad394233376c29a5081b6e136d90bd7303c17b/android/app/src/main/java/com/bluebubbles/messaging/workers/FCMWorker.java#L108-L109

Thanks for your answer.
So the idea is to run the "await" through a flutter engine ?
No other simpler way to avoid those crashes ?
Using a compute() for instance ?

@C4sse
Copy link

C4sse commented Jul 6, 2022

https://github.com/BlueBubblesApp/bluebubbles-app/blob/b7ad394233376c29a5081b6e136d90bd7303c17b/android/app/src/main/java/com/bluebubbles/messaging/workers/FCMWorker.java#L108-L109

could you please he,p on what you mean running await? do I add this to the flutter code or the dependency

@zlshames
Copy link
Author

Here is an updated link to how we use the engine, with our latest code base. I dont think much has changed, but maybe it'll help: https://github.com/BlueBubblesApp/bluebubbles-app/blob/5ae9a183734bf3de2107a95b3799429c49a85186/android/app/src/main/java/com/bluebubbles/messaging/workers/FCMWorker.java#L106-L112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants