7

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.

Michael2411
  • 91
  • 1
  • 1
  • 5

3 Answers3

15

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();
Mohamed Ali
  • 3,717
  • 1
  • 33
  • 39
11

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.
}
DNS
  • 851
  • 11
  • 19
3

You can install the permission handler plugin and then use it:

final PermissionStatus permission = await PermissionHandler()
                .checkPermissionStatus(PermissionGroup.location);
ilBarra
  • 796
  • 1
  • 8
  • 25