utils/platform_location/README.md
Krzysztof kuhy Rudnicki b8e2bf8813
Some checks are pending
file length / file-length (push) Waiting to run
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
platform_location tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
platform_location: keep deniedForever, race fixes
checkPermission now reports deniedForever after a past request (prefs
flag + no rationale), so an app's resume re-check keeps the state.
currentLocation asks every enabled provider at once: fused alone sat
out its 30 s timeout on the emulator while GPS had a fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 23:29:59 +01:00

2.1 KiB

platform_location

Android location through AOSP's LocationManager (androidx LocationManagerCompat, Apache-2.0), owned once for every app. It replaces geolocator: geolocator_android links com.google.android.gms:play-services-location (plus play-services-base, -basement and -tasks, all non-free) even with forceLocationManager: true. An APK built with this package has no com/google/android/gms classes.

Install

dependencies:
  platform_location:
    git:
      url: https://github.com/kuhyx/utils
      ref: platform_location-v0.1.0
      path: platform_location

Android plugins auto-register, in the UI engine and in background engines (a flutter_foreground_task service isolate) alike. The app declares ACCESS_COARSE_LOCATION and, for GPS precision, ACCESS_FINE_LOCATION.

API

import 'package:platform_location/platform_location.dart';

final loc = PlatformLocation.instance;
if (!await loc.isLocationEnabled()) { /* switch is off */ }
var perm = await loc.checkPermission();            // never prompts
if (perm != LocationPermissionState.granted) {
  perm = await loc.requestPermission(precise: false); // needs an Activity
}
final fix = await loc.currentLocation();           // throws PlatformException
final sub = loc.fixes(interval: const Duration(seconds: 2)).listen((f) {});
  • deniedForever: refused and Android shows no rationale any more. A request reports it directly; checkPermission reports it only after this app has asked before (a prefs flag) and only with a foreground Activity, so an app's resume re-check keeps the state. Background engines see denied.
  • Provider: GPS when fine location is granted and GPS is on; else the platform fused provider (Android 12+, AOSP), else network. No Play services fused provider.
  • currentLocation falls back to the newest last-known fix, then throws no_fix. Stream errors: unavailable, denied, provider_disabled.
  • LocationFix: time (UTC), latitude, longitude, altitude (0 when the provider has none), accuracy (m, 0 when unknown), isMock.

Tests replace PlatformLocation.instance with a fake subclass.