currentPosition method

  1. @override
Future<PlatformFix> currentPosition({
  1. required LocationAccuracy accuracy,
  2. required Duration timeout,
  3. Duration? maxAge,
})
override

One fix. Reuses a native fix younger than maxAge. Answers within timeout with a fix or a timeout/noFix failure.

Implementation

@override
Future<PlatformFix> currentPosition({
  required LocationAccuracy accuracy,
  required Duration timeout,
  Duration? maxAge,
}) async {
  currentPositionCalls++;
  // Let concurrent callers join before answering, as a real request
  // would take time.
  await Future<void>.delayed(Duration.zero);
  // With maxAge, the OS's cached fix is returned as is, unfiltered, like
  // geolocator's getLastKnownPosition.
  final os = osLastKnown;
  if (maxAge != null &&
      os != null &&
      access.isAllowed &&
      os.ageReliable &&
      os.age <= maxAge) {
    return PlatformFix.success(
      os.position,
      age: os.age,
      source: LocationSource.cached,
    );
  }
  if (_hangNext > 0) {
    _hangNext--;
    return Completer<PlatformFix>().future;
  }
  if (_failNext.isNotEmpty) {
    return PlatformFix.failed(LocationFailure.of(_failNext.removeFirst()));
  }
  if (!access.isAllowed) {
    return PlatformFix.failed(
      LocationFailure.of(LocationFailureReason.permissionDenied),
    );
  }
  if (!serviceEnabled) {
    return PlatformFix.failed(
      LocationFailure.of(LocationFailureReason.serviceDisabled),
    );
  }
  final p = currentFix;
  if (p == null) {
    return PlatformFix.failed(
      LocationFailure.of(LocationFailureReason.timeout),
    );
  }
  _offerStore(p);
  return PlatformFix.success(p);
}