iosflutterxcodesimulator

Flutter Denied Permission Errors


i have a problem with flutter i could not solve it i tried to clean flutter and restart computer everything but still i have this error.

(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


Solution

  • Add Permissions to Info.plist: Open your Info.plist file and add the necessary keys for location permissions. For example: 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>
    

    Use the permission_handler package to request location permissions. First, add the package to your pubspec.yaml:

    dependencies:
      permission_handler: ^10.2.0
    

    Handle permission status:

    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();
      }
    }