Flutter 颤振-拒绝后重新询问位置许可
我目前正在使用location软件包检索用户的当前位置。如果用户拒绝此请求,我希望能够稍后在应用程序中重新请求这些权限,当需要他们的位置时Flutter 颤振-拒绝后重新询问位置许可,flutter,permissions,geolocation,location,Flutter,Permissions,Geolocation,Location,我目前正在使用location软件包检索用户的当前位置。如果用户拒绝此请求,我希望能够稍后在应用程序中重新请求这些权限,当需要他们的位置时 initPlatformState() async { await _locationService.changeSettings( accuracy: LocationAccuracy.HIGH, interval: 1000); LocationData location; // Platform messages
initPlatformState() async {
await _locationService.changeSettings(
accuracy: LocationAccuracy.HIGH, interval: 1000);
LocationData location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
if (_permission) {
location = await _locationService.getLocation();
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
initPlatformState();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
error = e.message;
print(error);
} else if (e.code == 'SERVICE_STATUS_ERROR') {
error = e.message;
print(error);
}
location = null;
}
setState(() {
_currentLocation = location;
});
}
上面的代码在应用程序的init状态下运行,并且总是在第一次运行应用程序时执行
在第一次运行时,当权限屏幕出现时,如果您授予权限并根据需要检索位置,则所有操作都会按预期进行。如果拒绝该权限,则权限将如预期的那样为“false”。但是,如果重新打开应用程序,它只会检索到“Service Status:True”
,并且卡在线路上
_permission = await _locationService.requestPermission();
再也没有进展了。我希望在执行上述代码时,“请共享您的位置”屏幕会重新出现,但似乎是因为用户在第一次启动时拒绝了它,所以它不会再次出现。因此,在更改之前,无法使用该应用程序
有没有办法使此消息重新显示?或者我必须指示用户打开设置并启用位置权限吗?您可以将
initPlatformState()包装到initState中的setState中
@override
void initState() {
super.initState();
setState(() {
initPlatformState()
});
}