Yukams / background_locator_fixed

A Flutter plugin for updating location in background.
MIT License
50 stars 94 forks source link

When app is terminated, location does not get updates, although service is running. #88

Closed IKRAMULHAQ786 closed 1 year ago

IKRAMULHAQ786 commented 1 year ago

I configured my project using example project and then customizing according to my scenario. My Scenario: Location is updates every 1 minute and is saved into a list. when the list entries == 10, (after every 10 minutes), the API is called which sends those 10 location coordinates to the database.

Problem: When app gets terminated, the location service is still running as I can see location icon & sticky notification but it does not receives new location updates and eventually API is not called.

Pubspec.yaml fluttertoast: ^8.2.2 shared_preferences: ^2.1.1 get: ^4.6.5 http: ^1.0.0 flutter_spinkit: ^5.2.0 background_locator_2: ^2.0.6 path_provider: ^2.0.15 location_permissions: ^4.0.1

Code Snippet where I did some customizations for my scenario, the rest of the code is same as Example project.

port.listen( (dynamic data) async { await updateUI(data);

    //! I suppose we can handle the api call here
    if (data != null) {
      log('data is received from port');
      LocationDto locationDto = LocationDto.fromJson(data);

      log('location added to locations');
      locations.add(LocationModel(
          latitude: locationDto.latitude,
          longitude: locationDto.longitude));

      log('Locations Length: ${locations.length}');

      //! after 1 minute location is added to list, so 10 locations mean 10 minutes are passed
      if (locations.length == 10) {
        locationApi();
      }
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        setState(() {});
      });
    }
  },
);

any help will be highly appreciated 
IKRAMULHAQ786 commented 1 year ago

@tulioccalazans

revrkk commented 1 year ago

I'm also facing the same issue. Is there any breakthrough @IKRAMULHAQ786

IKRAMULHAQ786 commented 1 year ago

Yes Actually, instead of writing the code in the listner, I wrote my API calling code in the Callback method. it works like a charm. if you want to do something when the app is terminated, you should write that part of code in callback method located in location_service_repository.dart file

tulioccalazans commented 1 year ago

Hello, I have two applications and I'm not having these kind of problems. Everything works perfectly.

@revrkk @IKRAMULHAQ786

Do you have any source code that I can analyze to try to help you?

IKRAMULHAQ786 commented 1 year ago

Sir as I mentioned earlier, I just copied the example project files and just wrote the code in port.listen();

where I tried to call the api every 10 mintues and send the location updates to the server.

But As soon As I terminate the app, the API is never called in the backgraound and server doesn't receive any updates.

then I called the api from callback method whch is in location_service_repository.dart file of Example project and now it works

IKRAMULHAQ786 commented 1 year ago

I didn't know where to write the code according to my scenario. I suppose they should have mentioned that how and where can we write our own code to handle the location updates