Getting "Unhandled Exception: User denied permissions to access the device's location"

1,001

The following step by step process might help :

pubspec.yaml

Add geolocator: ^8.0.1 under dependencies:

dependencies:
  flutter:
    sdk: flutter
 
  cupertino_icons: ^1.0.2
  geolocator: ^8.0.1

AndroidManifest.xml

You will see the following :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.get_weather">

Add the following under the above code:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.hardware.location.gps" />

main.dart

import 'package:flutter/material.dart';
import 'package:get_weather/screens/loading_screen.dart';
 
void main() => runApp(MyApp());
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: LoadingScreen(),
    );
  }
}

loading_screen.dart

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
 
class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}
 
class _LoadingScreenState extends State<LoadingScreen> {
  void getLocation() async {
    bool serviceEnabled;
    LocationPermission permission;
 
    // Test if location services are enabled.
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // Location services are not enabled don't continue
      // accessing the position and request users of the
      // App to enable the location services.
      return Future.error('Location services are disabled.');
    }
 
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        // Permissions are denied, next time you could try
        // requesting permissions again (this is also where
        // Android's shouldShowRequestPermissionRationale
        // returned true. According to Android guidelines
        // your App should show an explanatory UI now.
        return Future.error('Location permissions are denied');
      }
    }
 
    if (permission == LocationPermission.deniedForever) {
      // Permissions are denied forever, handle appropriately.
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }
 
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
 
    print(position);
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            //Get the current location
            getLocation();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}

The code in loading_screen.dart will help you get the 'User denied permission' resolved.

Share:
1,001
Debasis Sil
Author by

Debasis Sil

I have just started to learn to code. This is a new journey I have embarked upon to fulfill some of my dreams. I am a Financial Advisor for over 20 years and my work gives me enough freedom to travel (which is why I live) but not enough to pursue my dream wholeheartedly. I am hoping this new venture will help me to pursue my dream of a nomad and also fetch me enough to sustain :)

Updated on January 03, 2023

Comments

  • Debasis Sil
    Debasis Sil over 1 year

    After trying hard and spending a lot of time I found the following solution that worked for me: