問題描述
Flutter 地理定位器,清單中未定義位置權限。確保清單中至少定義了 ACCESS_FINE_LOCATION 或.... (Flutter geolocator, No location permissions defined in manifest. Make sure at least ACCESS_FINE_LOCATION or.... are defined in the manifest)
我在顫振中使用地理定位器,我在清單文件中定義了權限,但它一直告訴我我沒有。
清單文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="packagename">
<uses‑permission android:name="android.permission.INTERNET" />
<uses‑permission android:name="android.permission>ACCESS_COARSE_LOCATION" />
<uses‑permission android:name="android.permission>ACCESS_FINE_LOCATION" />
<uses‑permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
地理定位器代碼:
class _LoadingScreenState extends State<LoadingScreen> {
void getLocation() async{
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.deniedForever) {
return Future.error(
'Location permissions are permantly denied, we cannot request permissions.');
}
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission != LocationPermission.whileInUse &&
permission != LocationPermission.always) {
return Future.error(
'Location permissions are denied (actual value: $permission).');
}
}
Position position= await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
print(permission);
print(position);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: (){
getLocation();
},
child: Text('Get Location'),
),
),
);
}
}
我正在我的物理設備上測試應用程序。請告訴我我應該怎麼做才能使它起作用。
參考解法
方法 1:
The only thing wrong is the string inside "android.name".
<uses‑permission android:name="android.permission>ACCESS_COARSE_LOCATION" />
<uses‑permission android:name="android.permission>ACCESS_FINE_LOCATION" />
The correct is:
<uses‑permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses‑permission android:name="android.permission.ACCESS_FINE_LOCATION" />
方法 2:
Before getting any location ask for permission first:
await Geolocator.requestPermission();
(by hiba、user16096259、Valters Šverns)