Skip to content

How to use

Simon Irmancnik edited this page Oct 21, 2022 · 3 revisions

See the best practices here : Example app

The plugin registers a static callback for getting updates even when app is terminated. To use other plugins like path_provider in this callback follow the steps there : (use other plugins in callback)

To update UI, I use Isolate.

More info on Isolate : Isolates and Event Loops - Flutter in Focus

  1. Initialize plugin:
static const String _isolateName = "LocatorIsolate";
ReceivePort port = ReceivePort();

@override
  void initState() {
    super.initState();

    IsolateNameServer.registerPortWithName(port.sendPort, _isolateName);
    port.listen((dynamic data) {
      // do something with data
    });
    initPlatformState();
  }
  
Future<void> initPlatformState() async {
    await BackgroundLocator.initialize();
}
  1. Create the callback function, also the init- and notification callback if you need it:

Note: The pragma decorator is necessary and without it callbacks will not work with flutter 3.x

@pragma('vm:entry-point')
static void callback(LocationDto locationDto) async {
    final SendPort send = IsolateNameServer.lookupPortByName(_isolateName);
    send?.send(locationDto);
}

//Optional
@pragma('vm:entry-point')
static void initCallback(dynamic _) {
    print('Plugin initialization');
}

//Optional
@pragma('vm:entry-point')
static void notificationCallback() {
    print('User clicked on the notification');
}
  1. Start the location service :

Note: Before starting the plugin make sure to have the necessary permissions. See example code for more detail.

//Somewhere in your code
startLocationService();

void startLocationService(){
    BackgroundLocator.registerLocationUpdate(LocationCallbackHandler.callback,
        initCallback: LocationCallbackHandler.initCallback,
        initDataCallback: data,
        disposeCallback: LocationCallbackHandler.disposeCallback,
        autoStop: false,
        iosSettings: IOSSettings(
            accuracy: LocationAccuracy.NAVIGATION, distanceFilter: 0),
        androidSettings: AndroidSettings(
            accuracy: LocationAccuracy.NAVIGATION,
            interval: 5,
            distanceFilter: 0,
            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.',
                notificationIcon: '',
                notificationIconColor: Colors.grey,
                notificationTapCallback:
                    LocationCallbackHandler.notificationCallback)));
}

More detail on LocationSettings

  1. Unregister the service when you are done:
IsolateNameServer.removePortNameMapping(_isolateName);
BackgroundLocator.unRegisterLocationUpdate();