我遇到了 flutter 问题,我无法解决它,我尝试清理 flutter 并重新启动计算机,但仍然出现此错误。
(https://i.sstatic.net/6Hg3g2EB.png)
Future\<String\> getCurrentCity() async {
try {
Position position = await getLocation();
List\<Placemark\> placemarks = await placemarkFromCoordinates(position.latitude, position.longitude);
return placemarks[0].locality ?? "";
} catch (e) {
// Handle errors like services not enabled, etc.
throw Exception('Failed to get current city: $e');
}
}
Future\<void\> \_handleLocationPermissions() async {
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
if (permission == LocationPermission.deniedForever || permission == LocationPermission.denied) {
// Handle case where permissions are denied
throw Exception('Location permissions are denied');
}
}
}
\````
im new for flutter env so i cant solve this i tried so many ways but still nothing happens
在Info.plist中添加权限: 打开 Info.plist 文件并添加位置权限所需的密钥。例如: XML
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need your location to provide better services.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>We need your location to provide better services.</string>
使用permission_handler包来请求位置权限。首先,将包添加到您的 pubspec.yaml 中:
dependencies:
permission_handler: ^10.2.0
处理权限状态:
import 'package:permission_handler/permission_handler.dart';
Future<void> requestLocationPermission() async {
var status = await Permission.location.request();
if (status.isGranted) {
// Permission granted, proceed with accessing location
} else if (status.isDenied) {
// Permission denied, handle accordingly
} else if (status.isPermanentlyDenied) {
// Permission permanently denied, open app settings
openAppSettings();
}
}