How to implement to check Gps and location permission is enable or not.

416 views
Skip to first unread message

sumit kumawat

unread,
Jul 25, 2020, 6:33:57 AM7/25/20
to Flutter Development (flutter-dev)
Hi,
I want to build app in which require user current location to show near by places. for this i'm using location: ^3.0.0.
It's working fine but when user manually disable gps and location permission in that case i want to force fully ask request for location permission and gps until they are not grant permission.

please suggest how do it.please have look what i did.

showlocation() 
async {
try {
// bool geolocationStatus = await location.ser viceEnabled();
bool geolocationStatus1 = await location.serviceEnabled();
if (geolocationStatus1)
{
if(Theme.of(context).platform==TargetPlatform.android){
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.granted) {

var data = await location.getLocation();
setState(() {
_locationData=data;
});
} else {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted)
{
_permissionGranted = await location.requestPermission();
if (_permissionGranted == PermissionStatus.granted) {

var data = await location.getLocation();
setState(() {
_locationData=data;
});
}else{

}
}else{
var data = await location.getLocation();
setState(() {
_locationData=data;
});
}
}
}
} else {
if (!await location.requestService()) {
show_dialog(context,
'You have to enable location service to show all the near by places');
}else{
location.getLocation().then((data) {
setState(() {
_locationData = data;
});
print('_locationData $_locationData');
});
}
} catch (exception) {
print('exception is $exception');
}
}

Souvik Dutta

unread,
Jul 25, 2020, 7:25:35 AM7/25/20
to sumit kumawat, Flutter Development (flutter-dev)
First see which of the permission variables result to false when the user denies permission. Probably it is the location.permissionEnabled(). Then you can have an else statement surrounding this if statement that will show the required dialog. Like
if location.<the required func> {
   <do something>
} else {
   <show the required dialog>
}

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/6382e74d-a551-4e8e-ad6f-13eb4a7adb01o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages