ekasetiawans / flutter_background_service

264 stars 183 forks source link

E/EventChannel#lyokone/locationstream( 5498): Failed to open event stream E/EventChannel#lyokone/locationstream( 5498): java.lang.NullPointerException: Attempt to write to field 'io.flutter.plugin.common.EventChannel$EventSink com.lyokone.location.FlutterLocation.events' on a null object reference in method 'void com.lyokone.location.StreamHandlerImpl.onListen(java.lang.Object, io.flutter.plugin.common.EventChannel$EventSink)' E/EventChannel#lyokone/locationstream( 5498): at com.lyokone.location.StreamHandlerImpl.onListen(StreamHandlerImpl.java:50) #456

Open narzullayev0772 opened 2 months ago

narzullayev0772 commented 2 months ago
@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
  service.on('stop').listen((event) {
    LogService.i('Stopping the service');
    service.stopSelf();
  });

  LogService.i('Hello from the background mode');
  BackgroundService.location.onLocationChanged.listen((LocationData currentLocation) {
    LogService.i('Location changed: ${currentLocation.latitude}, ${currentLocation.longitude}');
    locator<AddLocationUseCase>().call(
        params: StartLocation(
      id: 192,
      latitude: currentLocation.latitude,
      longitude: currentLocation.longitude,
    ));
  });
}

what is wrong in my code

jestebto commented 1 month ago

I believe your issue has more to do with the flutter 'location' package than the 'flutter_background_location' package. I am currently stuck at the same one. I think 'location' is supposed to be run in the main isolate, and does not support running from a different isolate (your background/foreground service is a different isolate). The reason being that the location plugin probably depends on the flutter engine, which is not available in the service. I would be happy if somebody would correct me if I am wrong.