Exception has occurred. PermissionDeniedException (User denied permissions to access the device's location.)

Viewed 2123

Everythings are normal but it says:

Exception has occurred.
PermissionDeniedException (User denied permissions to access the device's location.)

And shows this line on debug: Position position = await Geolocator.getCurrentPosition( Here the permissions:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

This is the full code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'dart:async';

// ignore: constant_identifier_names
const double ZOOM = 1;

class HomeView extends StatelessWidget {
  static GoogleMapController? _googleMapController;
  Set<Marker> markers = Set();
  // ignore: prefer_final_fields, unused_field
  Completer<GoogleMapController> _controllerGoogleMap = Completer(); //BURADAN
  late GoogleMapController newGoogleMapController;

  late Position currentPosition;
  var geoLocator = Geolocator();
  double bottomPaddingOfMap = 0;

  void locatePosition() async {
    //USER SURRENT LOCATION
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    currentPosition = position;

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

    CameraPosition cameraPosition =
        // ignore: unnecessary_new
        new CameraPosition(target: latLatPosition, zoom: 14);
    newGoogleMapController
        .animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
  }

  // ignore: unnecessary_const, unused_field
  static const CameraPosition _kGooglePlex = const CameraPosition(
    target: LatLng(40.7956, 29.4420),
    zoom: 14.4746,
  );

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection("Location").snapshots(),
        builder: (context, snapshot) {
          // ignore: avoid_print
          print(snapshot);
          if (snapshot.hasData) {
            GeoPoint location = snapshot.data!.docs.first.get("location");
            if (location == null) {
              return const Text("There was no location data");
            }
            markers.clear();

            final latLng = LatLng(location.latitude, location.longitude);
            markers.add(
                Marker(
                  markerId: const MarkerId("location"),
                   position: latLng,
                   infoWindow: const InfoWindow(
                     title: 'dede',
                     snippet: 'deedci'
                   ),
                   ));  
            _googleMapController?.animateCamera(CameraUpdate.newCameraPosition(
              CameraPosition(
                target: latLng,
                zoom: ZOOM,
              ),
            ));

            return GoogleMap(
              padding: EdgeInsets.only(bottom: bottomPaddingOfMap),
              mapType: MapType.normal,
              myLocationButtonEnabled: true,
              markers: markers,
              initialCameraPosition: _kGooglePlex,
              myLocationEnabled: true,
              zoomGesturesEnabled: true,
              zoomControlsEnabled: true,
              onMapCreated: (GoogleMapController controller) {
                _controllerGoogleMap.complete(controller);
                newGoogleMapController = controller;

                locatePosition();
              },
            );
          }
          return const Center(
            child: CircularProgressIndicator(),
          );
        },
      ),
    );
  }
}
1 Answers

add these line before ((getCurrentPosition)) to make it ask for permission

bool serviceEnabled;
LocationPermission permission;

serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
  return Future.error('Location services are disabled');
}

permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
  permission = await Geolocator.requestPermission();
  if (permission == LocationPermission.denied) {
    return Future.error('Location permissions are denied');
  }
}

if (permission == LocationPermission.deniedForever) {
  return Future.error(
      'Location permissions are permanently denied, we cannot request permissions.');
}
Related