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

i am trying to get user current location in flutter but its showing map, but not pointing my current location. I have added all required permissions in AndriodManifest file.

here is snap

enter image description here

here are the logs

I/Google Maps Android API(24140): Google Play services package version: 201817022
E/GoogleMapController(24140): Cannot enable MyLocation layer as location permissions are not granted
D/HostConnection(24140): HostConnection::get() New Host Connection established 0xefab0ec0, tid 25110
E/flutter (24140): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: User denied permissions to access the device's location.
E/flutter (24140): #0      MethodChannelGeolocator._handlePlatformException
package:geolocator_platform_interface/…/implementations/method_channel_geolocator.dart:242
E/flutter (24140): #1      MethodChannelGeolocator.getCurrentPosition
package:geolocator_platform_interface/…/implementations/method_channel_geolocator.dart:124
E/flutter (24140): <asynchronous suspension>
E/flutter (24140): #2      _locationState.locatepostion
package:map_app/abc.dart:26
E/flutter (24140): <asynchronous suspension>
W/System  (24140): A resource failed to call release.

code

  Completer<GoogleMapController> _controllerGoogleMap=Completer();
  late GoogleMapController newGoogleMapController;
  double mapbottompadding=0;

  GlobalKey<ScaffoldState> scaffoldkey=new GlobalKey<ScaffoldState>();
  late Position currentpositon;
  var geolocator=Geolocator();

  void locatepostion() async{
    Position position=await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    currentpositon=position;  // this is line 26, it is point before await

    LatLng latLngPosition=LatLng(position.latitude,position.longitude);

    CameraPosition cameraPosition=new CameraPosition(target: latLngPosition,zoom: 14);
    newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
  }

  static final CameraPosition googlepostion=CameraPosition(target: LatLng(37.4249,-122.0657));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: [
        GoogleMap(
          padding: EdgeInsets.only(bottom: mapbottompadding),
          mapType: MapType.normal,
          myLocationButtonEnabled: true,
          myLocationEnabled: true,
          zoomControlsEnabled: true,
          zoomGesturesEnabled: true,
           initialCameraPosition: googlepostion,
          onMapCreated: (GoogleMapController controller){
              _controllerGoogleMap.complete(controller);
              newGoogleMapController=controller;
              setState(() {
                mapbottompadding=300.0;
              });         
              locatepostion();
         

and here is my output

enter image description here

-------Update

it's working perfectly on andriod phone, but not working on emulator. and i want to ask how can i resize the map, i am using width and height but not working.



Solution 1:[1]

 LocationPermission permission;
   permission = await Geolocator.requestPermission();

request for the permission first by using this.

Solution 2:[2]

class GeolocatorService {
  Future<Position?> determinePosition() async {
    LocationPermission permission;
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.deniedForever) {
        return Future.error('Location Not Available');
      }
    } else {
      throw Exception('Error');
    }
    return await Geolocator.getCurrentPosition();
  }
}

Solution 3:[3]

This is a common issues in geolocator that arises esp. when you don't grant permission to the device. By default it is disabled, so you have to request for it when the locatePosition() is called. Remember that the geolocator package is wrapped under google_maps_flutter. So to solve the location problem, ensure you do the following

class _MapScreenState extends State<MapScreen> {
void locatePosition() async {
    bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
    
    await Geolocator.checkPermission();
    await Geolocator.requestPermission();
    
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    
    currentPosition = position;
    LatLng latLngPosition = LatLng(position.latitude, position.longitude);
        
    // Ask permission from device
    Future<void> requestPermission() async {
    await Permission.location.request();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Riyazat Durrani
Solution 2
Solution 3