fluttergeolocationgoogle-maps-android-api-2

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.


Solution

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

    request for the permission first by using this.