Skip to content

Commit

Permalink
close StreamController in LocationMarkerDataStreamFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tlserver committed Aug 15, 2023
1 parent c4717f5 commit 81d9a90
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/src/data_stream_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ class LocationMarkerDataStreamFactory {
Geolocator.requestPermission,
}) {
final List<AsyncCallback> cancelFunctions = [];
final streamController = StreamController<Position?>.broadcast(
onCancel: () =>
Future.wait(cancelFunctions.map((callback) => callback())),
);
final streamController = StreamController<Position?>.broadcast();
streamController.onListen = () async {
try {
LocationPermission permission = await Geolocator.checkPermission();
Expand All @@ -70,14 +67,20 @@ class LocationMarkerDataStreamFactory {
switch (permission) {
case LocationPermission.denied:
case LocationPermission.deniedForever:
if (streamController.isClosed) {
break;
}
streamController.sink
..addError(const lm.PermissionDeniedException())
..close();
.addError(const lm.PermissionDeniedException());
streamController.close();
case LocationPermission.whileInUse:
case LocationPermission.always:
try {
final serviceEnabled =
await Geolocator.isLocationServiceEnabled();
if (streamController.isClosed) {
break;
}
if (!serviceEnabled) {
streamController.sink
.addError(const ServiceDisabledException());
Expand All @@ -97,12 +100,19 @@ class LocationMarkerDataStreamFactory {
} catch (_) {}
try {
final lastKnown = await Geolocator.getLastKnownPosition();
if (streamController.isClosed) {
break;
}
if (lastKnown != null) {
streamController.sink.add(lastKnown);
}
} catch (_) {}
try {
streamController.sink.add(await Geolocator.getCurrentPosition());
final position = await Geolocator.getCurrentPosition();
if (streamController.isClosed) {
break;
}
streamController.sink.add(position);
} catch (_) {}
final subscription =
Geolocator.getPositionStream().listen((position) {
Expand All @@ -116,6 +126,10 @@ class LocationMarkerDataStreamFactory {
streamController.sink.addError(const IncorrectSetupException());
}
};
streamController.onCancel = () async {
Future.wait(cancelFunctions.map((callback) => callback()));
await streamController.close();
};
return streamController.stream;
}

Expand Down

0 comments on commit 81d9a90

Please sign in to comment.