I'm asking users to grant permission for location, but I came to know that this permission is used to access location if location is enabled on the phone. I actually want to scan bluetooth devices and I'm using flutter blue plus package.
await Permission.location.isGranted;
So I want to check whether location is enabled, if not I want a pop up asking the user for the location in a similar manner to how we ask for bluetooth.
So I want it to be done on a flutter project.
I'm expecting a clear cut solution.
Thanks in advance.
try this snippet of code:
import 'package:geolocator/geolocator.dart';
import 'package:nb_utils/nb_utils.dart';
Future<Position> checkLocationPremission() async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
LocationPermission permission = await Geolocator.checkPermission();
if (!serviceEnabled) {
// do what you want
}
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
toast('Please location permission');
logger.w("get User LocationPosition()");
await Geolocator.openAppSettings();
throw '${language.lblLocationPermissionDenied}';
}
}
if (permission == LocationPermission.deniedForever) {
throw "language lbl Location Permission Denied Permanently, please enable it from setting";
}
return await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high).then((value) {
return value;
}).catchError((e) async {
return await Geolocator.getLastKnownPosition().then((value) async {
if (value != null) {
return value;
} else {
throw "lbl Enable Location";
}
}).catchError((e) {
toast(e.toString());
});
});
}