getCurrentPosition method
Implementation
@override
Future<Position> getCurrentPosition({
LocationSettings? locationSettings,
}) async {
final settings = locationSettings ?? const LocationSettings();
final client = await _getOrCreateClient();
final completer = Completer<Position>();
StreamSubscription<DBusSignal>? subscription;
final signals = DBusRemoteObjectSignalStream(
object: client,
interface: _clientIface,
name: 'LocationUpdated',
signature: DBusSignature('oo'),
);
subscription = signals.listen(
(signal) async {
try {
final newPath = signal.values[1] as DBusObjectPath;
final position = await _readLocation(newPath);
if (!completer.isCompleted) completer.complete(position);
await subscription?.cancel();
await _stopClient(client);
} catch (e) {
if (!completer.isCompleted) completer.completeError(e);
}
},
onError: (Object e, StackTrace st) {
if (!completer.isCompleted) completer.completeError(e, st);
},
);
try {
await _startClient(client, _accuracyToGeoClueLevel(settings.accuracy));
} catch (e) {
await subscription.cancel();
rethrow;
}
if (settings.timeLimit != null) {
return completer.future.timeout(
settings.timeLimit!,
onTimeout: () async {
await subscription?.cancel();
await _stopClient(client);
throw TimeoutException(
'getCurrentPosition timed out',
settings.timeLimit,
);
},
);
}
return completer.future;
}