start method

Future<void> start({
  1. required LiveLocationConfig config,
  2. required void onLocation(
    1. LocationDto
    ),
  3. void onError(
    1. String error
    )?,
})

Start live location tracking with server sync

Parameters:

  • config: Configuration for tracking and sync behavior
  • onLocation: Callback invoked for each location update
  • onError: Optional callback invoked when errors occur

Throws StateError if already tracking Throws Exception if location permissions are denied

Implementation

Future<void> start({
  required LiveLocationConfig config,
  required void Function(LocationDto) onLocation,
  void Function(String error)? onError,
}) async {
  if (_isTracking) {
    throw StateError('Location tracking is already active');
  }

  if (!_isInitialized) {
    await initialize();
  }

  _currentConfig = config;
  _onLocation = onLocation;
  _onError = onError;

  try {
    // Register location updates with the native service
    await BackgroundLocator.registerLocationUpdate(
      _handleLocationUpdate,
      initCallback: _handleInit,
      initDataCallback: config.toMap(),
      disposeCallback: _handleDispose,
      androidSettings: config.androidSettings,
      iosSettings: config.iosSettings,
    );

    _isTracking = true;
    debugPrint('Live location tracking started');
  } catch (e) {
    _onError?.call('Failed to start tracking: $e');
    debugPrint('Failed to start location tracking: $e');
    rethrow;
  }
}