maghraby
maghraby

Reputation: 47

Flutter How to handle when user deny GPS permission or doesn't turn on the GPS for google maps

I'm using Google Maps package for displaying google map on my application. by default it asks for a permission to let the application use the GPS and when the permission is granted but the user has the GPS turned off, it asks them to turn it on.
Currently, i don't know how to handle these two sitations when the user denies the GPS permission or when the user choose 'No thanks' and doesn't turn on the GPS on their device. Therfore, it crashes the app for an unhandled exception.
Exception has occurred. _CastError (Null check operator used on a null value)

I want to make it handle these two situations by showing a widget that stating any of these situations rather than crashing the application.

Location Package (Where it asks for the Permission)

import 'package:geolocator/geolocator.dart';

class Location {
double? latitude;
double? longitude;

static LocationPermission? permission;

Future<void> getCurrentLocation() async {
permission = await Geolocator.requestPermission();
try {
  Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.low);
  latitude = position.latitude;
  longitude = position.longitude;
} catch (e) {
  print(e);
   }
 }
}

Loading Screen (Where it retrieve the current location data from GeoLocator package)

class _NearbyMechanicLoadingState extends 
State<NearbyMechanicLoading> {
bool isPopAllowed = false;
Location myLocation = Location();

void loadingData() async {
   await myLocation.getCurrentLocation();
   Navigator.pushReplacement(
    context,
    MaterialPageRoute(
      builder: (context) => NavigatingPage(
        title: 'Nearby Mechanics',
        page: NearbyMechanicPage(
          lat: myLocation.latitude,
          long: myLocation.longitude,
        ),
      ),
    ));
  }

 @override
 void initState() {
 super.initState();
 loadingData();
}

Google map screen

class _NearbyMechanicPageState extends State<NearbyMechanicPage> {
GoogleMapController? _googleMapController;

@override
void dispose() {
   _googleMapController!.dispose();
   super.dispose();
 } 

@override
Widget build(BuildContext context) {
    return GoogleMap(
       mapType: MapType.terrain,
       myLocationButtonEnabled: false,
       zoomControlsEnabled: false,
       initialCameraPosition:
       CameraPosition(target: LatLng(widget.lat!, widget.long!), 
       zoom:15.0),
       onMapCreated: (controller) => _googleMapController = controller,
      );
    }
   }

Upvotes: 1

Views: 2033

Answers (1)

William Verhaeghe
William Verhaeghe

Reputation: 326

You can use the permission_handler package to ask for permission and see if they gave permission.

If the user (permanently) denied permission, you can display a dialog/screen mentioning that they need to change the permission in settings. openAppSettings.

If the user didn't turn on their location, you can display a dialog/screen mentioning that they need to turn on their location. You can use Geolocator.openLocationSettings(); for this.

Upvotes: 3

Related Questions