Flutter - 拒绝后重新询问位置权限
Flutter - Re ask location permission after denied
我目前正在使用位置包来检索用户的当前位置。如果用户拒绝此请求,我希望稍后在需要他们的位置时能够在应用程序中重新请求这些权限。
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;
});
}
以上代码 运行 处于应用程序的初始状态,并且始终在首次 运行 应用程序时执行。
首先 运行,当权限屏幕出现时,如果您授予权限,一切都会按预期进行,并根据需要检索位置。如果您拒绝该权限,则权限为 "false",正如预期的那样。但是,如果您重新打开应用程序,它只会检索 "Service Status: True"
并卡在
行
_permission = await _locationService.requestPermission();
并且再也没有进步。我期待在执行上面的代码时,"Please share your location" 屏幕会重新出现,但似乎因为用户在第一次启动时拒绝了它,所以它不会重新出现。因此,在更改之前无法使用该应用程序。
有没有办法让这条消息重新出现?还是我必须指示用户打开设置并启用位置权限?
您可以将 initPlatformState()
包装在 initState
的 setState 中
@override
void initState() {
super.initState();
setState(() {
initPlatformState()
});
}
我目前正在使用位置包来检索用户的当前位置。如果用户拒绝此请求,我希望稍后在需要他们的位置时能够在应用程序中重新请求这些权限。
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;
});
}
以上代码 运行 处于应用程序的初始状态,并且始终在首次 运行 应用程序时执行。
首先 运行,当权限屏幕出现时,如果您授予权限,一切都会按预期进行,并根据需要检索位置。如果您拒绝该权限,则权限为 "false",正如预期的那样。但是,如果您重新打开应用程序,它只会检索 "Service Status: True"
并卡在
_permission = await _locationService.requestPermission();
并且再也没有进步。我期待在执行上面的代码时,"Please share your location" 屏幕会重新出现,但似乎因为用户在第一次启动时拒绝了它,所以它不会重新出现。因此,在更改之前无法使用该应用程序。
有没有办法让这条消息重新出现?还是我必须指示用户打开设置并启用位置权限?
您可以将 initPlatformState()
包装在 initState
@override
void initState() {
super.initState();
setState(() {
initPlatformState()
});
}