You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

离线状态下无法通过Geolocator包获取位置的问题求助

无网络环境下定位失败问题

我正在使用Geolocator.getCurrentPosition()方法实现定位,代码如下:

Future<Position> getCurrentPosition({
  LocationSettings? locationSettings,
  LocationAccuracy desiredAccuracy = LocationAccuracy.best,
  bool forceAndroidLocationManager = false,
  Duration? timeLimit,
}) async {
  LocationSettings? settings;
  if (locationSettings != null) {
    settings = locationSettings;
  } else if (defaultTargetPlatform == TargetPlatform.android) {
    settings = AndroidSettings(
      accuracy: desiredAccuracy,
      forceLocationManager: forceAndroidLocationManager,
    );
  }
  try {
    settings ??= LocationSettings(
      accuracy: desiredAccuracy,
      timeLimit: timeLimit,
    );
    debugPrint("await starts");
    Position position = await Geolocator.getCurrentPosition(
      locationSettings: settings,
    ).then(
      (value) {
        debugPrint("await stop");
        return value;
      },
    );

    return position;
  } catch (e) {
    debugPrint(e.toString());
    return Geolocator.getCurrentPosition(
      locationSettings: settings,
    );
  }
}

我也尝试过使用Location等其他定位包,但在无网络状态下仍然无法获取设备位置。


内容的提问来源于stack exchange,提问作者Omkar Borse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:18:14