How to use callback in flutter for background location

255

Callbacks are used to perform some actions during certain events.

Here, the callback attribute takes backgroundLocationCallBack as its value, and as per the package code, it is used for performing appropriate actions when the LocationDto is available. This LocationDto object will contain the Location information you are looking for, and you can use it according to your use-case.

Similarly, disposeCallback is used to perform certain actions when dispose is called on the BackgroundLocator object. You can do something like close all the Ports that were opened for fetching the location.

There are other callbacks as well such as initCallback and initDataCallback which are used for setting the callbacks to initialise various parameters for your location service and to provide an initial data for the params respectively.

Share:
255
Pannam
Author by

Pannam

Updated on January 04, 2023

Comments

  • Pannam
    Pannam over 1 year

    I am following this plugin (learning) background locator . I have previously used other background location plugins but they are not maintained anymore.

    I am following the tutorial as described in the description of the plugin and so far this is my code. I am unsure about what exactly a callback is and how do I use it.

    This is my code so far but I don't get any results.

    static void backgroundLocationCallBack(LocationDto location) async {
        await BackgroundLocator.initialize();
        await BackgroundLocator.isServiceRunning();
      }
    
      void getLocationLiveUpdates() async {
        return await BackgroundLocator.registerLocationUpdate(
            backgroundLocationCallBack,
            autoStop: false,
            // disposeCallback: backgroundLocationDisposeCallBack,
    
            androidSettings: AndroidSettings(
                accuracy: LocationAccuracy.NAVIGATION,
                interval: 5,
                distanceFilter: 0,
                client: LocationClient.google,
                androidNotificationSettings: AndroidNotificationSettings(
                  notificationChannelName: 'Location tracking',
                  notificationTitle: 'Start Location Tracking',
                  notificationMsg: 'Track location in background',
                  notificationBigMsg:
                      'Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running.',
                  notificationIconColor: Colors.grey,
                )));
      }
    
    • Srujan Chowdary Panda
      Srujan Chowdary Panda almost 2 years
      Facing few issues in working with background_locator. Can anyone provide me with a working example for tracking background location if the issue's resolved. reference: stackoverflow.com/questions/72246232/…
  • Pannam
    Pannam about 2 years
    Thank you, makes sense now, Could you please help me with the code, I am trying to print the location (latitude & longitude). I have been at it for weeks but so far unable to do so.
  • Admin
    Admin about 2 years
    try to print the location object like this : location.toString() in the callback method. If you read through the class code for LocationDto , you can find all the data members that are available for the object. github.com/rekabhq/background_locator/blob/master/lib/…
  • Srujan Chowdary Panda
    Srujan Chowdary Panda almost 2 years
    I've been facing similar issues in working with background_locator. Can anyone provide me with a working example for tracking background location if the issue's resolved. reference: stackoverflow.com/questions/72246232/…