I have problem when user entered google map page without location enabled the map wont updated so i want to check if the user have location enabled beforehand so i need function that return true if location enabled and false if not.
use permission handler plugin
ServiceStatus serviceStatus = await PermissionHandler().checkServiceStatus(PermissionGroup.location);
bool enabled = (serviceStatus == ServiceStatus.enabled);
you can also show a rationale for requesting permission (Android only)
bool isShown = await PermissionHandler().shouldShowRequestPermissionRationale(PermissionGroup.location);
or Location Plugin
var location = Location();
bool enabled = await location.serviceEnabled();
and request to enable it by
bool gotEnabled = await location.requestService();
for those who came to this stackoverflow question like myself, there have been changes and updates on PermissionHandler package and the older solutions are now invalid.
you can check if the location service is enabled:
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
// Use location.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With