riverpod
riverpod copied to clipboard
Flutter Geolocator PositionStream stops working in android studio emulator
Until yesterday the Geolocator positionStream was working just fine. I can't figure out why the stream does not produce any values even when I use the Extended Controls Tool to change the location of the device.
Here the riverpod position stream provider
@riverpod
Stream<Position> positionStream(
PositionStreamRef ref,
) async* {
final locationService = ref.watch(locationServiceProvider);
yield* locationService.geolocator.getPositionStream(
locationSettings: locationService.getLocationSettings(),
);
}
And the location service has this method for the settings.
LocationSettings getLocationSettings() {
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidSettings(
distanceFilter: 50,
//(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'),
));
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return AppleSettings(
accuracy: LocationAccuracy.best,
distanceFilter: AppLocationSettings.locationChangeDistance,
activityType: ActivityType.automotiveNavigation,
pauseLocationUpdatesAutomatically: true,
// Only set to true if our app will be started up in the background.
showBackgroundLocationIndicator: true,
);
} else {
return const LocationSettings(
accuracy: LocationAccuracy.best,
distanceFilter: 50,
);
}
}
I am using another provider to listen to the positionStreamProvider:
@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,
);
}
This is the provider which is not updating anymore. It is just annoying to spend hours just to figure out why it just stops working. Maybe I am missing something.
I start the provider by doing the following in one of the screens of the app.
final locationAsync = ref.watch(
positionStreamProvider.select((value) => value.whenData((value) => true)),
);
Basically I would use the Extended Controls Tool of the emulator to change the location and the positionStrean would yield a new value. But that is not happening anymore. The location is change on the map, but nothing happens with positionStream. I can't see any error logs. Most of the code come from the deliverzler app.
It did work when I change the myLocationCameraPositionProvider to the following:
@riverpod
CameraPosition
myLocationCameraPosition(MyLocationCameraPositionRef ref) {
final myLocation =
ref.watch(positionStreamProvider).valueOrNull;
return CameraPosition(
target: LatLng(
myLocation?.latitude ?? defaultLatLng.latitude,
myLocation?.longitude ?? defaultLatLng.longitude,
),
zoom: defaultMapZoom,
);
}
I don't understand why this version is working and the first one not.