如何使用 Geolocator 插件检测 Flutter 中的模拟位置

Fai*_*her 5 android geolocation android-location flutter

我正在尝试检测模拟位置。我已经安装了假位置应用程序并在“设置”(在“开发人员选项”下)中启用了“模拟位置”。但是当我尝试使用Geolocator检测模拟位置时,它根本不起作用。

Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;

serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
  return Future.error('Location services are disabled.');
}

permission = await Geolocator.checkPermission();
if (permission == LocationPermission.deniedForever) {
  return Future.error(
      'Location permissions are permanently denied, we cannot request permissions.');
}

if (permission == LocationPermission.denied) {
  permission = await Geolocator.requestPermission();
  if (permission != LocationPermission.whileInUse &&
      permission != LocationPermission.always) {
    return Future.error(
        'Location permissions are denied (actual value: $permission).');
  }
}
return await Geolocator.getCurrentPosition(
    desiredAccuracy: LocationAccuracy.high);
}
Run Code Online (Sandbox Code Playgroud)

然后我将此函数称为:

_determinePosition().then((position) {
  if (position.isMocked) {
    //.. Show error message
    return;
  }
Run Code Online (Sandbox Code Playgroud)

并且这个 if 块永远不会到达并且也没有错误日志。