skip to Main Content

I’m using this plugin: permission_handler: ^10.4.3
Because I need to scan for nearby WiFi networks, which requires location permission. However, in my existing project that I’ve.

I’ve simultaneously applied for location, locationWhenInUse, and locationAlways permissions, but only the locationAlways permission is taking effect.

However, in a newly created project, I am able to request all of these permissions successfully.

enter image description here
enter image description here

2

Answers


  1. For permission to handle things, you may need to write some platform-specific code into their respective Android/iOS folder.

    Android (write in AndroidManifest.xml):

    <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" />
    

    iOS (write in Info.plist):

    <key>NSLocationUsageDescription</key>
    <string>We require your location to provide you with turn-by-turn navigation and track your route history.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>We need access to your location to offer location-based reminders and geofencing services.</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>We need access to your location to provide you with personalized weather forecasts and recommend nearby attractions.</string>
    

    Reference link: https://pub.dev/packages/permission_handler

    Login or Signup to reply.
  2. I tested this permission handling on Pixel 6a (API 33) Android 13. & it is working as documented & expected. That says that depending on the platform & version, the result may differ or vary.

    Android (write in AndroidManifest.xml):

    <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" />
    

    Lib (write in any .dart file):

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key});
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                commonButton(
                  text: "Ask for Location",
                  onPressed: permissionLocation,
                ),
                commonButton(
                  text: "Ask for Location Always",
                  onPressed: permissionLocationAlways,
                ),
                commonButton(
                  text: "Ask for Location When In Use",
                  onPressed: permissionLocationWhenInUse,
                ),
              ],
            ),
          ),
        );
      }
    
      Widget commonButton({required String text, required Function() onPressed}) {
        return SizedBox(
          width: MediaQuery.of(context).size.width / 1.5,
          child: ElevatedButton(
            onPressed: onPressed,
            child: Text(text),
          ),
        );
      }
    
      Future<bool> permissionLocation() async {
        bool hasPermission = false;
        final PermissionStatus t0 = await Permission.location.status;
        if (t0 == PermissionStatus.granted) {
          hasPermission = true;
        } else {
          final PermissionStatus t1 = await Permission.location.request();
          if (t1 == PermissionStatus.granted) {
            hasPermission = true;
          } else {}
        }
        return Future<bool>.value(hasPermission);
      }
    
      Future<bool> permissionLocationAlways() async {
        bool hasPermission = false;
        final PermissionStatus t0 = await Permission.locationAlways.status;
        if (t0 == PermissionStatus.granted) {
          hasPermission = true;
        } else {
          final PermissionStatus t1 = await Permission.locationAlways.request();
          if (t1 == PermissionStatus.granted) {
            hasPermission = true;
          } else {}
        }
        return Future<bool>.value(hasPermission);
      }
    
      Future<bool> permissionLocationWhenInUse() async {
        bool hasPermission = false;
        final PermissionStatus t0 = await Permission.locationWhenInUse.status;
        if (t0 == PermissionStatus.granted) {
          hasPermission = true;
        } else {
          final PermissionStatus t1 = await Permission.locationWhenInUse.request();
          if (t1 == PermissionStatus.granted) {
            hasPermission = true;
          } else {}
        }
        return Future<bool>.value(hasPermission);
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search