代码要求位置,用户批准,代码抛出错误:
PlatformException(PERMISSION_DENIED,访问位置数据被拒绝,为null)
但是在第二次请求该职位后,将立即返回正确的职位,而不会出现对话框或错误。
返回位置,并且在请求位置和用户批准时不会引发任何错误。
import 'package:geolocator/geolocator.dart';
try{
position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
catch(error)
{
print("error in geolocation: $error"); //error in geolocation: PlatformException(PERMISSION_DENIED, Access to location data denied, null)
print("position: $position"); // position: null
print("retrying"); // retrying
try{
position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
catch(error)
{
await printGeoStatus(5);
print("2nd error in geolocation $error");
}
print("final position: $position"); //flutter: final position: Lat: 59.xyz, Long: 17.xyz
}
Flutter版本: “ Channel dev,v1.5.8”
地理位置版本: ^ 4.0.2
平台:
@FlashJonas最近,我们已经完全重构了geolocator
插件(版本5.0.0),我想知道您是否在最新版本中仍然遇到此问题?
@mvanbeusekom我确认问题仍然存在于5.0.0中
刚刚通过AppBrewery课程发现了您的软件包。
我决定按照软件包文档进行操作,并将3个键添加到我的plist文件中:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
首次致电
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
提示用户许可(有3个选择):
如果我按“仅在使用应用程序时”,则终端记录以下内容:
[VERBOSE-2:ui_dart_state.cc(148)]未处理的异常:PlatformException(PERMISSION_DENIED,访问位置数据被拒绝,为空)
0 Geolocator._handleInvalidPermissions(软件包:geolocator / geolocator.dart:191:7)
1 Geolocator.getCurrentPosition(包:geolocator / geolocator.dart:102:7)
2 _LoadingScreenState.getLocation(package:clima / screens / loading_screen.dart:13:10)
3 _LoadingScreenState.build。
(package:clima / screens / loading_screen.dart:23:13) 4 _InkResponseState._handleTap(包:flutter / src / material / ink_well.dart:511:14)
5 _InkResponseState.build。
(包装:flutter / src / material / ink_well.dart:566:30) 6 GestureRecognizer.invokeCallback(包:flutter / src / gestures / recognizer.dart:166:24)
7 TapGestureRecognizer._checkUp(包:flutter / src / gestures / tap.dart:240:9)
8 TapGestureRecognizer.handlePrimaryPointer(包:flutter / src / gestures / tap.dart:177:9)
9 <…>
第二次按下就可以,并且位置记录没有问题:
颤振:纬度:37.785834,长:-122.406417
代码 :
使用geolocator 5.1.2在我的iPhone 5s 12.2上仍然存在相同的问题。
*编辑(Flutter Channel开发,v1.8.4)
// Called after closing welcome alert
Future getFirstLocation() async {
Position position;
try {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
} catch (error) {
print("getfirstlocation error 1");
try {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
} catch (error) {
print("error in geolocation $error");
}
}
print("getfirstlocation position: $position");
if (position != null) {
// send to database
}
}
运行以上代码的日志:
flutter: getfirstlocation error 1
flutter: getfirstlocation position: Lat: 59.xxxx, Long: 17.yyyy
我遇到同样的错误:
Android(Oneplus 2)-Android 8.1.0-Geolocator 5.1.2
(授予位置权限)
2019-08-15 16:54:22.606 926-1110/system_process E/InputDispatcher: channel '569829a de.paschke.coffeebreak/de.paschke.coffeebreak.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-08-15 16:54:56.808 14735-14766/? E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(PERMISSION_DENIED, Access to location data denied, null)
#0 Geolocator._handleInvalidPermissions (package:geolocator/geolocator.dart:207:7)
#1 Geolocator.getLastKnownPosition (package:geolocator/geolocator.dart:146:7)
<asynchronous suspension>
#2 LocationService.getLastKnownLocation (package:coffee_break/services/location_service.dart:24:51)
<asynchronous suspension>
#3 UserStoreBase.checkLastLocation (package:coffee_break/stores/user_store.dart:125:36)
使用所有三个权限字符串时,出现相同的错误。 如果仅使用NSLocationWhenInUseUsageDescription,则该应用程序将再次运行。 我最好的猜测是,当使用所有三个权限时,该应用程序将启动并在要求用户确认之前先询问位置。 ui的行为似乎可以证实这一点,但这只是一个猜测。 无论如何,如果您只能使用NSLocationWhenInUseUsageDescription,则请删除其他两个。
相同的问题:(在颤振的最新版本稳定通道上。
这个包裹糟透了...
同样的错误,Android 10上的最新版本的Flutter。
我有同样的问题!
我想知道这个问题是否会得到解决,我认为它已经存在一年多了,人们仍然会面对它。 最近,我开始学习Flutter,并遇到了相同的问题,并且复制的步骤完全相同。
当我从iOS提示符中选择“仅在使用应用程序时使用”选项时,即使在第二次从应用程序请求位置时,Flutter控制台也会出现异常,我会获得正确的位置数据。
PS使用最新的5.3.2+2
版本
我目前正在针对此问题进行修复,并列出了其他问题(有关更多详细信息,请参见#430)。 不过,这将是一个重大更新。
目前,iOS方面已经准备就绪,并且现在可以在Android方面使用。
此问题应在6.0.0版本中修复
回复可能太晚了,但我也遇到了同样的问题,即该应用程序未在iOS中请求许可,并且在android中运行正常。
因为没有询问权限,所以权限代码不适用于iOS。 我找到了一个名为“ location_permissions”的程序包,该程序包可用于手动请求权限。
步骤如下
将包导入文件中
导入' package:location_permissions / location_permissions.dart ';
在您要寻求许可的页面上添加以下代码。 (最好在应用程序的第一页上添加它。)
<strong i="16">@override</strong>
void initState() {
....
if (Platform.isIOS) {
location_permission();
}
....
}
在同一文件中添加以下两种方法
void location_permission()异步{
最终的PermissionStatus权限=等待_getLocationPermission();
if(permission == PermissionStatus.granted){
最终位置=等待geolocator.getCurrentPosition(
requiredAccuracy:LocationAccuracy.best);
// Use the position to do whatever...
}
}
未来
最后的PermissionStatus权限=等待LocationPermissions()
.checkPermissionStatus(level:LocationPermissionLevel.location);
if (permission != PermissionStatus.granted) {
final PermissionStatus permissionStatus = await LocationPermissions()
.requestPermissions(
permissionLevel: LocationPermissionLevel.location);
return permissionStatus;
} else {
return permission;
}
}
就是这样,现在您应该在iOS应用程序中弹出一个窗口,询问位置许可。
最有用的评论
我目前正在针对此问题进行修复,并列出了其他问题(有关更多详细信息,请参见#430)。 不过,这将是一个重大更新。
目前,iOS方面已经准备就绪,并且现在可以在Android方面使用。