flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

Duplicated onLocation listener and getting same location multiples times

Open guns28 opened this issue 2 years ago • 4 comments

Your Environment

Flutter (Channel stable, 3.0.5, on macOS 11.6.1 20G224 darwin-x64, locale fr) • Flutter version 3.0.5 at /Users/macbook/Downloads/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision f1875d570e (9 weeks ago), 2022-07-13 11:24:16 -0700 • Engine revision e85ea0e79c • Dart version 2.17.6 • DevTools version 2.12.2

this is called once on the splashscreen :

  • Plugin config: bg.BackgroundGeolocation.ready(bg.Config( desiredAccuracy: bg.Config.DESIRED_ACCURACY_NAVIGATION, disableElasticity:true, autoSync: true, stopOnTerminate: true, startOnBoot: false, disableStopDetection: true, enableHeadless: true, debug: false, allowIdenticalLocations:false, logLevel: bg.Config.LOG_LEVEL_VERBOSE ))
 
    bg.BackgroundGeolocation.onLocation((bg.Location location) {
      print('[location] - $location');
      _updateCurrentPosition(location);
    }, _onLocationError);

    final state = await bg.BackgroundGeolocation.state;
    if (state.enabled) {
      print('[state] - ${state.enabled}');
      return;
    }

    bg.BackgroundGeolocation.start().then((bg.State state) {
      print('[start] success $state');
    }).catchError((error) {
      print('[start] ERROR: $error');
    });

    bg.BackgroundGeolocation.changePace(true).then((bool isMoving) {
      print('[changePace] success $isMoving');
    }).catchError((e) {
      print('[changePace] ERROR: ' + e.code.toString());
    });
  

Expected Behavior

Each time i enter the recording screen i get only one instance

Actual Behavior

each time i enter the instance get duplicated and i'm having the same location multiple times

Steps to Reproduce

1.Enter recording screen 2.start location updates 3.exit and stop 4.back again

Context

Debug logs

Logs
      I/flutter (32180): [location] - [Location {odometer: 5216590.0, activity: {confidence: 100, type: still}, provider: {airplane: false, gps: false, accuracyAuthorization: 0, enabled: true, network: true, status: 3}, mock: true, extras: {}, event: providerchange, battery: {level: 0.54, is_charging: true}, uuid: 75ce772c-d518-425c-b55b-7e5f8346d0db, coords: {altitude: 118.3, heading: 1.0, latitude: 51.39497424, accuracy: 8.0, heading_accuracy: 0.1, altitude_accuracy: 0.1, speed_accuracy: 0.01, speed: 0.0, longitude: 10.40564339}, is_moving: true, timestamp: 2022-09-16T09:53:32.553Z}]

I/flutter (32180): [location] - [Location {odometer: 5216590.0, activity: {confidence: 100, type: still}, provider: {airplane: false, gps: false, accuracyAuthorization: 0, enabled: true, network: true, status: 3}, mock: true, extras: {}, event: providerchange, battery: {level: 0.54, is_charging: true}, uuid: 75ce772c-d518-425c-b55b-7e5f8346d0db, coords: {altitude: 118.3, heading: 1.0, latitude: 51.39497424, accuracy: 8.0, heading_accuracy: 0.1, altitude_accuracy: 0.1, speed_accuracy: 0.01, speed: 0.0, longitude: 10.40564339}, is_moving: true, timestamp: 2022-09-16T09:53:32.553Z}]

I/TSLocationManager(32180): [c.t.l.data.sqlite.b persist] 
I/TSLocationManager(32180):   ✅  INSERT: 75ce772c-d518-425c-b55b-7e5f8346d0db
I/flutter (32180): [location] - [Location {odometer: 5216590.0, activity: {confidence: 100, type: still}, provider: {airplane: false, gps: false, accuracyAuthorization: 0, enabled: true, network: true, status: 3}, mock: true, extras: {}, event: providerchange, battery: {level: 0.54, is_charging: true}, uuid: 75ce772c-d518-425c-b55b-7e5f8346d0db, coords: {altitude: 118.3, heading: 1.0, latitude: 51.39497424, accuracy: 8.0, heading_accuracy: 0.1, altitude_accuracy: 0.1, speed_accuracy: 0.01, speed: 0.0, longitude: 10.40564339}, is_moving: true, timestamp: 2022-09-16T09:53:32.553Z}]

I/flutter (32180): [location] - [Location {odometer: 5216590.0, activity: {confidence: 100, type: still}, provider: {airplane: false, gps: false, accuracyAuthorization: 0, enabled: true, network: true, status: 3}, mock: true, extras: {}, event: providerchange, battery: {level: 0.54, is_charging: true}, uuid: 75ce772c-d518-425c-b55b-7e5f8346d0db, coords: {altitude: 118.3, heading: 1.0, latitude: 51.39497424, accuracy: 8.0, heading_accuracy: 0.1, altitude_accuracy: 0.1, speed_accuracy: 0.01, speed: 0.0, longitude: 10.40564339}, is_moving: true, timestamp: 2022-09-16T09:53:32.553Z}]

D/TSLocationManager(32180): [c.t.l.service.AbstractService start] 
D/TSLocationManager(32180):   🎾  LocationRequestService [eventCount: 1]
I/TSLocationManager(32180): [c.t.l.l.SingleLocationRequest startUpdatingLocation] 
I/TSLocationManager(32180):   🔵  [SingleLocationRequest start, action: 3, requestId: 57]
D/TSLocationManager(32180): [c.t.l.service.AbstractService finish] ⚙️︎  finish LocationRequestService [eventCount: 0, sticky: true]
I/flutter (32180): New location added with id : 2014
I/flutter (32180): New location added with id : 2015
I/flutter (32180): New location added with id : 2016
I/flutter (32180): New location added with id : 2017

guns28 avatar Sep 16 '22 10:09 guns28

You likely have duplicate onLocation listeners.

every time you call onLocation, your callback is added to a list. When a location arrives, the List of callbacks are iterated and executed.

it is impossible to have “multiple instances” of BackgroundGeolocation.

christocracy avatar Sep 16 '22 12:09 christocracy

ok when i exit and come back how can i remove the old listener ?

guns28 avatar Sep 16 '22 13:09 guns28

Should i call it only once when the app start ? and then send location to the recording screen.

guns28 avatar Sep 16 '22 13:09 guns28

Should i call it only once when the app start

Yes. Listening to events is like wiring up speakers to your stereo system. You do it once. You don't disconnect them when you turn off your stereo.

Also see API docs .removeListener(Function) or .removeListeners().

christocracy avatar Sep 16 '22 14:09 christocracy

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 27 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 10 '24 01:06 github-actions[bot]