Flutter-geolocator: java.lang.NullPointerException when getting location in a separate isolate

Created on 18 Mar 2020  ·  6Comments  ·  Source: Baseflow/flutter-geolocator

🐛 Bug Report

I'm getting the error when trying to get location in background process so the plugin runs in a separate isolate using workmanager:

Failed to handle method call
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

Here is some stack trace:

E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234): Failed to handle method call
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.google.android.gms.common.GooglePlayServicesUtilLight.isGooglePlayServicesAvailable(Unknown Source:12)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.google.android.gms.common.GoogleApiAvailabilityLight.isGooglePlayServicesAvailable(Unknown Source:5)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source:94)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.google.android.gms.common.GoogleApiAvailabilityLight.isGooglePlayServicesAvailable(Unknown Source:3)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source:93)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.baseflow.googleapiavailability.GoogleApiAvailabilityPlugin.onMethodCall(GoogleApiAvailabilityPlugin.java:105)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#flutter.baseflow.com/google_api_availability/methods( 5234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Reproduction steps

I've created a sample app to reproduce the issue: https://github.com/estevez-dev/sample_flutter_app
It is preconfigured to run in Gitpod. Just tap floating button to start background task and you will get an exception on getCurrentPosition.
Please note that app will not request location permission, you need to allow it manually from Android settings.

Configuration

Plugin version: 5.3.0

Platform:

  • [ ] :iphone: iOS
  • [x] :robot: Android
android in progress bug

All 6 comments

Thank you very much for submitting this issue and the extensive code sample and error description. I will look into this and try to provide a fix soon.

i have the same issue do you find any solutions please

@walide67 , no solution yet.

@walide67 , no solution yet.

Do you have any solution to tracking location in backgrounf or foreground with flutter
Please any help because my work stoped

This issue should now be solved, we have release a hotfix for the google_api_availability plugin (which the geolocator depends on) solving the null pointer exception.

Since the google_api_availability was released as an hotfix it should automatically be picked up, but if you want to be sure please update to geolocator version 5.3.1.

Can confirm it works! Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings