我在用Geolocator
用于获取设备当前位置的插件,以及用于在 flutter 中集成地图小部件的 google 地图插件
谷歌地图工作正常,但Geolocator
它给了我这个错误:
D/permissions_handler(10148): No permissions found in manifest for: $permission
D/permissions_handler(10148): No permissions found in manifest for: $permission
并且错误仍然出现,知道为什么会发生这种情况吗?
并且在文件中Androidmanifest.xml
我在里面添加了这些权限
<manifest>:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
问题是 google_maps_flutter 包需要访问您的位置的权限,但该包没有本机代码来请求该权限。
因此,您需要编写本机代码或仅安装另一个能够获取该权限的包。
安装这个:https://pub.dartlang.org/packages/location
Then:
getLocationPermission() async {
final Location location = new Location();
try {
location.requestPermission(); //to lunch location permission popup
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
print('Permission denied');
}
}
}
或者,如果您想要更可靠的代码,这是我的某个项目的代码(带有位置包):
//Show some loading indicator depends on this boolean variable
bool askingPermission = false;
@override
void initState() {
this.getLocationPermission();
super.initState();
}
Future<bool> getLocationPermission() async {
setState(() {
this.askingPermission = true;
});
bool result;
final Location location = Location();
try {
if (await location.hasPermission())
result = true;
else {
result = await location.requestPermission();
}
print('getLocationPermission: '
'${result ? 'Access Allowed' : 'Access Denied'}');
} catch (log, trace) {
result = false;
print('getLocationPermission/log: $log');
print('getLocationPermission/trace: $trace');
} finally {
setState(() {
this.askingPermission = false;
});
}
return result;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)