liveLocation method

Stream<LocationData> liveLocation({
  1. String provider = 'best',
  2. int intervalMs = 2000,
  3. double distanceMeters = 0,
})

Starts continuous location updates and returns a broadcast Stream of LocationData objects. The stream stays open until stopLiveLocation is called or the widget is disposed.

  • provider: 'best' (default) registers all enabled providers and delivers whichever fires first. Use 'gps', 'network', or 'passive' to restrict to a single provider.
  • intervalMs: minimum time between updates in milliseconds (default 2000).
  • distanceMeters: minimum distance between updates in metres (default 0).

Example:

final sub = location.liveLocation(intervalMs: 1000).listen((fix) {
  print('${fix.lat}, ${fix.lng} via ${fix.provider}');
});

Implementation

Stream<LocationData> liveLocation({
  String provider = 'best',
  int intervalMs = 2000,
  double distanceMeters = 0,
}) {
  // Cast to the concrete implementation to access the raw event stream
  final impl = FlutterLocationPlusPlatform.instance as MethodChannelFlutterLocationPlus;
  // Tell native to start emitting updates with the requested parameters
  FlutterLocationPlusPlatform.instance.startLiveLocation(
    provider: provider,
    intervalMs: intervalMs,
    distanceMeters: distanceMeters,
  );
  // Map raw CSV strings → LocationData, dropping any unparseable frames
  return impl.liveLocationRawStream
      .map(LocationData.tryParse)
      .where((d) => d != null)
      .cast<LocationData>();
}