3

I want to fetch the GeoLocation of a device, in background, even when the app is terminated. Flutter provides Isolates, along with AlarmManager, as a way to do so.

My end-goal, again, is to get GeoLocation of a device even when the application has been terminated.

I have gone through https://medium.com/flutter-io/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124 but did not understand it completely, as I am a Novice in Flutter. If this is the only way, please help me understand it.

I am using the plugins :- https://pub.dartlang.org/packages/geolocator https://pub.dartlang.org/packages/flutter_isolate

The problem comes, as I see, when Geolocator tries to get location after checking GoogleApiAvailability Plugin internally, using a 'Channel.invokeMethod' inside a Secondary Isolate. The error it throws is :-

[  +81 ms] E/GooglePlayServicesUtil(11608): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608): Failed to handle method call
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608): 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(11608):     at com.google.android.gms.common.GooglePlayServicesUtilLight.isGooglePlayServicesAvailable(Unknown Source:12)
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608):     at com.google.android.gms.common.GoogleApiAvailabilityLight.isGooglePlayServicesAvailable(Unknown Source:5)
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608):     at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source:94)

Have a look at :- https://github.com/BaseflowIT/flutter-geolocator/blob/b869f3221c890c2ae87b22d0384577fd902d38ca/lib/geolocator.dart#L75

which calls GoogleApiAvailability at :- https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/lib/src/google_api_availability.dart#L23

This is the line that crashes on a Secondary Isolate.

The geolocation plugin has an additional configuration, where I can force LocationManager to jump in. That doesn't crash, but hangs up as soon as app is terminated. Following is how it is forced in the geolocator plugin :- https://github.com/BaseflowIT/flutter-geolocator/blob/b869f3221c890c2ae87b22d0384577fd902d38ca/lib/geolocator.dart#L64

This flag can be set before invoke the getLocation method.

If this won't work, I need help as to how to achieve my goal.

Ideally it should give me the Location on a Secondary Isolate. I need to understand the correct configurations required for the same.

user3279692
  • 329
  • 2
  • 13

1 Answers1

0

GoogleApiAvailability plugin uses current activity to check if API is available, see here: https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityPlugin.java#L50

I suppose this is wrong because isGooglePlayServicesAvailable() method needs Context, not Activity (see: https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability).

Activity is only needed to request API here: https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityPlugin.java#L67

This will not work for isolate(or service) so if activity is not available plugin shall use Application context instead.