StreamProvider does not update when app in background

52 Views Asked by At

I am working on an app with Geolocator and Riverpod. This is my Position stream provider:

@riverpod
Stream<Position> positionStream(
   PositionStreamRef ref,
) async* {
  final locationService = ref.watch(locationServiceProvider);

  yield* GeolocatorPlatform.instance.getPositionStream(
  locationSettings: locationService.getLocationSettings(),
 );
}

and the location service settings:

AndroidSettings(
      distanceFilter: AppLocationSettings.locationChangeDistance,
      //(Optional) Set foreground notification config to keep the app alive
      //when going to the background
      foregroundNotificationConfig: const ForegroundNotificationConfig(
        notificationTitle: "Running in Background",
        notificationText:
            "Example app will continue to receive your location even when you aren't 
   using it",
        notificationIcon: AndroidResource(name: 'notification_icon'),
      ));

I am also watching the stream provider as follows:

final myLocation = ref.watch(myLocationCameraPositionProvider);
debugPrint('myLocation: $myLocation');

with myLocationCameraPositionProvider as follows:

@riverpod
CameraPosition 
  myLocationCameraPosition(MyLocationCameraPositionRef ref) {
  final myLocation =
  ref.watch(positionStreamProvider.select((value) => 
      value.valueOrNull));

  return CameraPosition(
    target: LatLng(
        myLocation?.latitude ?? defaultLatLng.latitude,
        myLocation?.longitude ?? defaultLatLng.longitude,
    ),
    zoom: defaultMapZoom,
  );
  }

But when I change the location in android studio with the app in background, nothing happens. Only when the app gains the focus again that I see the debug message. On the emulator device I have set the 'Allow all time' permission.

0

There are 0 best solutions below